1c5e268c6Sopenharmony_ci/* 2c5e268c6Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3c5e268c6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4c5e268c6Sopenharmony_ci * you may not use this file except in compliance with the License. 5c5e268c6Sopenharmony_ci * You may obtain a copy of the License at 6c5e268c6Sopenharmony_ci * 7c5e268c6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8c5e268c6Sopenharmony_ci * 9c5e268c6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10c5e268c6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11c5e268c6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12c5e268c6Sopenharmony_ci * See the License for the specific language governing permissions and 13c5e268c6Sopenharmony_ci * limitations under the License. 14c5e268c6Sopenharmony_ci */ 15c5e268c6Sopenharmony_ci 16c5e268c6Sopenharmony_ci/** 17c5e268c6Sopenharmony_ci * @addtogroup NNRt 18c5e268c6Sopenharmony_ci * @{ 19c5e268c6Sopenharmony_ci * 20c5e268c6Sopenharmony_ci * @brief Provides a unified interface for AI chip drivers to access OpenHarmony.\n 21c5e268c6Sopenharmony_ci * Neural Network Runtime (NNRt) is a cross-chip inference computing runtime environment\n 22c5e268c6Sopenharmony_ci * oriented to the AI field. 23c5e268c6Sopenharmony_ci * 24c5e268c6Sopenharmony_ci * @since 3.2 25c5e268c6Sopenharmony_ci * @version 2.0 26c5e268c6Sopenharmony_ci */ 27c5e268c6Sopenharmony_ci 28c5e268c6Sopenharmony_ci/** 29c5e268c6Sopenharmony_ci * @file INnrtDevice.idl 30c5e268c6Sopenharmony_ci * 31c5e268c6Sopenharmony_ci * @brief Defines methods related to chip devices. 32c5e268c6Sopenharmony_ci * 33c5e268c6Sopenharmony_ci * You can use the methods to query chip device information and build AI models. 34c5e268c6Sopenharmony_ci * 35c5e268c6Sopenharmony_ci * @since 3.2 36c5e268c6Sopenharmony_ci * @version 2.0 37c5e268c6Sopenharmony_ci */ 38c5e268c6Sopenharmony_ci 39c5e268c6Sopenharmony_ci/** 40c5e268c6Sopenharmony_ci * @brief Defines the package path of the NNRt module. 41c5e268c6Sopenharmony_ci * 42c5e268c6Sopenharmony_ci * @since 3.2 43c5e268c6Sopenharmony_ci * @version 2.0 44c5e268c6Sopenharmony_ci */ 45c5e268c6Sopenharmony_cipackage ohos.hdi.nnrt.v2_0; 46c5e268c6Sopenharmony_ci 47c5e268c6Sopenharmony_ciimport ohos.hdi.nnrt.v2_0.NnrtTypes; 48c5e268c6Sopenharmony_ciimport ohos.hdi.nnrt.v2_0.ModelTypes; 49c5e268c6Sopenharmony_ciimport ohos.hdi.nnrt.v2_0.IPreparedModel; 50c5e268c6Sopenharmony_ci 51c5e268c6Sopenharmony_ci/** 52c5e268c6Sopenharmony_ci * @brief Provides methods for device management and model building. 53c5e268c6Sopenharmony_ci * 54c5e268c6Sopenharmony_ci * When multiple devices are registered, ensure that the combination of the device name\n 55c5e268c6Sopenharmony_ci * and vendor name is globally unique. 56c5e268c6Sopenharmony_ci * 57c5e268c6Sopenharmony_ci * @since 3.2 58c5e268c6Sopenharmony_ci * @version 2.0 59c5e268c6Sopenharmony_ci */ 60c5e268c6Sopenharmony_ciinterface INnrtDevice { 61c5e268c6Sopenharmony_ci /** 62c5e268c6Sopenharmony_ci * @brief Obtains the device name. 63c5e268c6Sopenharmony_ci * 64c5e268c6Sopenharmony_ci * @param name Device name. 65c5e268c6Sopenharmony_ci * 66c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 67c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 68c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 69c5e268c6Sopenharmony_ci */ 70c5e268c6Sopenharmony_ci GetDeviceName([out] String name); 71c5e268c6Sopenharmony_ci 72c5e268c6Sopenharmony_ci /** 73c5e268c6Sopenharmony_ci * @brief Obtains the device vendor name. 74c5e268c6Sopenharmony_ci * 75c5e268c6Sopenharmony_ci * @param name Device vendor name. 76c5e268c6Sopenharmony_ci * 77c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 78c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 79c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 80c5e268c6Sopenharmony_ci */ 81c5e268c6Sopenharmony_ci GetVendorName([out] String name); 82c5e268c6Sopenharmony_ci 83c5e268c6Sopenharmony_ci /** 84c5e268c6Sopenharmony_ci * @brief Obtains the device type. 85c5e268c6Sopenharmony_ci * 86c5e268c6Sopenharmony_ci * @param deviceType Device type. For details, see {@link DeviceType}. 87c5e268c6Sopenharmony_ci * 88c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 89c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 90c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 91c5e268c6Sopenharmony_ci */ 92c5e268c6Sopenharmony_ci GetDeviceType([out] enum DeviceType deviceType); 93c5e268c6Sopenharmony_ci 94c5e268c6Sopenharmony_ci /** 95c5e268c6Sopenharmony_ci * @brief Obtains the device status. 96c5e268c6Sopenharmony_ci * 97c5e268c6Sopenharmony_ci * @param deviceType Device status. For details, see {@link DeviceStatus}. 98c5e268c6Sopenharmony_ci * 99c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 100c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 101c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 102c5e268c6Sopenharmony_ci */ 103c5e268c6Sopenharmony_ci GetDeviceStatus([out] enum DeviceStatus status); 104c5e268c6Sopenharmony_ci 105c5e268c6Sopenharmony_ci /** 106c5e268c6Sopenharmony_ci * @brief Obtains the device's support for the operators of the specified AI model. 107c5e268c6Sopenharmony_ci * 108c5e268c6Sopenharmony_ci * @param model AI model. For details, see {@link Model}. 109c5e268c6Sopenharmony_ci * @param ops Operators supported and not supported by the device. The operators are listed in the same\n 110c5e268c6Sopenharmony_ci * sequence as they listed in the API model. 111c5e268c6Sopenharmony_ci * 112c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 113c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 114c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 115c5e268c6Sopenharmony_ci */ 116c5e268c6Sopenharmony_ci GetSupportedOperation([in] struct Model model, [out] boolean[] ops); 117c5e268c6Sopenharmony_ci 118c5e268c6Sopenharmony_ci /** 119c5e268c6Sopenharmony_ci * @brief Checks whether the device supports the Float32 model with the Float16 precision. 120c5e268c6Sopenharmony_ci * 121c5e268c6Sopenharmony_ci * @param isSupported Whether the Float16 precision is supported. 122c5e268c6Sopenharmony_ci * 123c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 124c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 125c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 126c5e268c6Sopenharmony_ci */ 127c5e268c6Sopenharmony_ci IsFloat16PrecisionSupported([out] boolean isSupported); 128c5e268c6Sopenharmony_ci 129c5e268c6Sopenharmony_ci /** 130c5e268c6Sopenharmony_ci * @brief Checks whether the device supports performance preference settings. For details about the performance\n 131c5e268c6Sopenharmony_ci * preference, see {@link PerformanceMode}. 132c5e268c6Sopenharmony_ci * 133c5e268c6Sopenharmony_ci * @param isSupported Whether performance preference settings are supported. 134c5e268c6Sopenharmony_ci * 135c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 136c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 137c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 138c5e268c6Sopenharmony_ci */ 139c5e268c6Sopenharmony_ci IsPerformanceModeSupported([out] boolean isSupported); 140c5e268c6Sopenharmony_ci 141c5e268c6Sopenharmony_ci /** 142c5e268c6Sopenharmony_ci * @brief Checks whether the device supports task priority settings. For details about the priority,\n 143c5e268c6Sopenharmony_ci * see {@link Priority}. 144c5e268c6Sopenharmony_ci * 145c5e268c6Sopenharmony_ci * @param isSupported Whether task priority settings are supported. 146c5e268c6Sopenharmony_ci * 147c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 148c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 149c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 150c5e268c6Sopenharmony_ci */ 151c5e268c6Sopenharmony_ci IsPrioritySupported([out] boolean isSupported); 152c5e268c6Sopenharmony_ci 153c5e268c6Sopenharmony_ci /** 154c5e268c6Sopenharmony_ci * @brief Checks whether the device supports dynamic input, which allows a model of different shapes\n 155c5e268c6Sopenharmony_ci * to be used for different operations. 156c5e268c6Sopenharmony_ci * 157c5e268c6Sopenharmony_ci * If dynamic input is supported, <b>-1</b> is added in the shape of the input tensor. 158c5e268c6Sopenharmony_ci * 159c5e268c6Sopenharmony_ci * @param isSupported Whether dynamic input is supported. 160c5e268c6Sopenharmony_ci * 161c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 162c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 163c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 164c5e268c6Sopenharmony_ci */ 165c5e268c6Sopenharmony_ci IsDynamicInputSupported([out] boolean isSupported); 166c5e268c6Sopenharmony_ci 167c5e268c6Sopenharmony_ci /** 168c5e268c6Sopenharmony_ci * @brief Builds a model. 169c5e268c6Sopenharmony_ci * 170c5e268c6Sopenharmony_ci * If the AI model supports dynamic input, at least one dimension of the input tensor contains <b>-1</b>. 171c5e268c6Sopenharmony_ci * 172c5e268c6Sopenharmony_ci * @param model Module to build. For details, see {@link Model}. 173c5e268c6Sopenharmony_ci * @param config Model configuration. For details, see {@link ModelConfig}. 174c5e268c6Sopenharmony_ci * @param preparedModel Model object built. For details, see {@link IPreparedModel}. 175c5e268c6Sopenharmony_ci * 176c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 177c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 178c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 179c5e268c6Sopenharmony_ci */ 180c5e268c6Sopenharmony_ci PrepareModel([in] struct Model model, [in] struct ModelConfig config, [out] IPreparedModel preparedModel); 181c5e268c6Sopenharmony_ci 182c5e268c6Sopenharmony_ci /** 183c5e268c6Sopenharmony_ci * @brief Checks whether the device supports caching of the AI models built. 184c5e268c6Sopenharmony_ci * 185c5e268c6Sopenharmony_ci * If yes, <b>PrepareModelFromModelCache()</b> and <b>ExportModelCache()</b> need to be implemented. 186c5e268c6Sopenharmony_ci * 187c5e268c6Sopenharmony_ci * @param isSupported Whether caching of the AI models built is supported. 188c5e268c6Sopenharmony_ci * 189c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 190c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 191c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 192c5e268c6Sopenharmony_ci */ 193c5e268c6Sopenharmony_ci IsModelCacheSupported([out] boolean isSupported); 194c5e268c6Sopenharmony_ci 195c5e268c6Sopenharmony_ci /** 196c5e268c6Sopenharmony_ci * @brief Loads an AI model from the cache. The AI model is exported by using <b>ExportModelCache()</b>. 197c5e268c6Sopenharmony_ci * 198c5e268c6Sopenharmony_ci * @param modelCache Array of the model files, which are in the same sequence as they exported. For details,\n 199c5e268c6Sopenharmony_ci * see {@link SharedBuffer}. 200c5e268c6Sopenharmony_ci * @param config Configuration for loading the model. For details, see {@link ModelConfig}. 201c5e268c6Sopenharmony_ci * @param preparedModel Model object. For details, see {@link IPreparedModel}. 202c5e268c6Sopenharmony_ci * 203c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 204c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 205c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 206c5e268c6Sopenharmony_ci */ 207c5e268c6Sopenharmony_ci PrepareModelFromModelCache([in] struct SharedBuffer[] modelCache, [in] struct ModelConfig config, 208c5e268c6Sopenharmony_ci [out] IPreparedModel preparedModel); 209c5e268c6Sopenharmony_ci 210c5e268c6Sopenharmony_ci /** 211c5e268c6Sopenharmony_ci * @brief Loads offline model files from the cache. The offline model is obtained after offline model files\n 212c5e268c6Sopenharmony_ci * are passed by the inference framework to the NNRt module and then parsed by the NNRt module. 213c5e268c6Sopenharmony_ci * 214c5e268c6Sopenharmony_ci * @param offlineModels Array of offline model files. The sequence of array elements depends on the format of\n 215c5e268c6Sopenharmony_ci * the input offline model. For details about the element types, see the definition of SharedBuffer {@link SharedBuffer}. 216c5e268c6Sopenharmony_ci * @param config Configuration for loading the offline model files. For details, see {@link ModelConfig}. 217c5e268c6Sopenharmony_ci * @param preparedModel Model object obtained. For details, see {@link IPreparedModel}. 218c5e268c6Sopenharmony_ci * 219c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 220c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 221c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 222c5e268c6Sopenharmony_ci */ 223c5e268c6Sopenharmony_ci PrepareOfflineModel([in] struct SharedBuffer[] offlineModels, [in] struct ModelConfig config, 224c5e268c6Sopenharmony_ci [out] IPreparedModel preparedModel); 225c5e268c6Sopenharmony_ci 226c5e268c6Sopenharmony_ci /** 227c5e268c6Sopenharmony_ci * @brief Allocates the shared memory for the device. The shared memory allows quick access to the input\n 228c5e268c6Sopenharmony_ci * and output data for AI inference. 229c5e268c6Sopenharmony_ci * 230c5e268c6Sopenharmony_ci * @param length Length of the shared memory to allocate, in bytes. 231c5e268c6Sopenharmony_ci * @param buffer Information about the shared memory allocated, including the file descriptor and size.\n 232c5e268c6Sopenharmony_ci * For details, see {@link SharedBuffer}. 233c5e268c6Sopenharmony_ci * 234c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 235c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 236c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 237c5e268c6Sopenharmony_ci */ 238c5e268c6Sopenharmony_ci AllocateBuffer([in] unsigned int length, [out] struct SharedBuffer buffer); 239c5e268c6Sopenharmony_ci 240c5e268c6Sopenharmony_ci /** 241c5e268c6Sopenharmony_ci * @brief Releases the shared memory. 242c5e268c6Sopenharmony_ci * 243c5e268c6Sopenharmony_ci * @param buffer Information about the shared memory allocated, including the file descriptor and size.\n 244c5e268c6Sopenharmony_ci * For details, see {@link SharedBuffer}. 245c5e268c6Sopenharmony_ci * 246c5e268c6Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful. 247c5e268c6Sopenharmony_ci * @return Returns a non-0 value if the operation fails. A negative value is an HDF standard error code,\n 248c5e268c6Sopenharmony_ci * and a positive value is a dedicated error code defined by NNRt. For details, see {@link NNRT_ReturnCode}. 249c5e268c6Sopenharmony_ci */ 250c5e268c6Sopenharmony_ci ReleaseBuffer([in] struct SharedBuffer buffer); 251c5e268c6Sopenharmony_ci} 252c5e268c6Sopenharmony_ci 253c5e268c6Sopenharmony_ci/** @} */ 254