1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { Callback } from './@ohos.base'; 17 18/** 19 * @namespace mindSporeLite 20 * @syscap SystemCapability.AI.MindSporeLite 21 * @stagemodelonly 22 * @since 10 23 */ 24declare namespace mindSporeLite { 25 /** 26 * Create a Model instance from file path 27 * @param { string } model - model indicates model path to be loaded 28 * @param { Context } context - context indicates model context information 29 * @returns { Promise<Model> } the promise returned by the function. 30 * @syscap SystemCapability.AI.MindSporeLite 31 * @stagemodelonly 32 * @since 10 33 */ 34 function loadModelFromFile( 35 model: string, 36 context?: Context): Promise<Model>; 37 38 /** 39 * Create a Model instance from file path. 40 * @param { string } model - model indicates model path to be loaded 41 * @param { callback: Callback<Model> } callback - the callback of model 42 * @syscap SystemCapability.AI.MindSporeLite 43 * @stagemodelonly 44 * @since 10 45 */ 46 function loadModelFromFile( 47 model: string, callback: Callback<Model>): void; 48 49 /** 50 * Create a Model instance from file path. 51 * @param { string } model - model indicates model path to be loaded 52 * @param { Context } [context] - context indicates model context information 53 * @param { callback: Callback<Model> } callback - the callback of model 54 * @syscap SystemCapability.AI.MindSporeLite 55 * @stagemodelonly 56 * @since 10 57 */ 58 function loadModelFromFile( 59 model: string, 60 context: Context, callback: Callback<Model>): void; 61 62 /** 63 * Create a Model instance from buffer 64 * @param { ArrayBuffer } model - model indicates model buffer to be loaded 65 * @param { Context } context - context indicates model context information 66 * @returns { Promise<Model> } the promise returned by the function. 67 * @syscap SystemCapability.AI.MindSporeLite 68 * @stagemodelonly 69 * @since 10 70 */ 71 function loadModelFromBuffer( 72 model: ArrayBuffer, 73 context?: Context): Promise<Model>; 74 75 /** 76 * Create a Model instance from buffer 77 * @param { ArrayBuffer } model - model indicates model buffer to be loaded 78 * @param { callback: Callback<Model> } callback - the callback of model 79 * @syscap SystemCapability.AI.MindSporeLite 80 * @stagemodelonly 81 * @since 10 82 */ 83 function loadModelFromBuffer( 84 model: ArrayBuffer, callback: Callback<Model>): void; 85 86 /** 87 * Create a Model instance from buffer 88 * @param { ArrayBuffer } model - model indicates model buffer to be loaded 89 * @param { Context } [context] - context indicates model context information 90 * @param { callback: Callback<Model> } callback - the callback of model 91 * @syscap SystemCapability.AI.MindSporeLite 92 * @stagemodelonly 93 * @since 10 94 */ 95 function loadModelFromBuffer( 96 model: ArrayBuffer, 97 context: Context, callback: Callback<Model>): void; 98 99 /** 100 * Creates a Model instance file description 101 * @param { number } model - model indicates model file description to be loaded 102 * @param { Context } context - context indicates model context information 103 * @returns { Promise<Model> } the promise returned by the function. 104 * @syscap SystemCapability.AI.MindSporeLite 105 * @stagemodelonly 106 * @since 10 107 */ 108 function loadModelFromFd( 109 model: number, 110 context?: Context): Promise<Model>; 111 112 /** 113 * Create a Model instance from file description 114 * @param { number } model - model indicates model file description to be loaded 115 * @param { callback: Callback<Model> } callback - the callback of model 116 * @syscap SystemCapability.AI.MindSporeLite 117 * @stagemodelonly 118 * @since 10 119 */ 120 function loadModelFromFd( 121 model: number, callback: Callback<Model>): void; 122 123 /** 124 * Create a Model instance from file description 125 * @param { number } model - model indicates model file description to be loaded 126 * @param { Context } [context] - context indicates model context information 127 * @param { callback: Callback<Model> } callback - the callback of model 128 * @syscap SystemCapability.AI.MindSporeLite 129 * @stagemodelonly 130 * @since 10 131 */ 132 function loadModelFromFd( 133 model: number, 134 context: Context, callback: Callback<Model>): void; 135 136 /** 137 * Provides manages model function. Including get inputs, predict ,resize. 138 * @typedef Model 139 * @syscap SystemCapability.AI.MindSporeLite 140 * @stagemodelonly 141 * @since 10 142 */ 143 interface Model { 144 /** 145 * Get model input tensors. 146 * @returns { MSTensor[] } the MSTensor array of the inputs. 147 * @syscap SystemCapability.AI.MindSporeLite 148 * @stagemodelonly 149 * @since 10 150 */ 151 getInputs(): MSTensor[]; 152 153 /** 154 * Infer model 155 * @param { MSTensor[] } inputs - indicates the MSTensor array of the inputs. 156 * @param { callback: Callback<MSTensor[]> } callback - the callback of MSTensor array. 157 * @syscap SystemCapability.AI.MindSporeLite 158 * @stagemodelonly 159 * @since 10 160 */ 161 predict(inputs: MSTensor[], callback: Callback<MSTensor[]>): void; 162 163 /** 164 * Infer model 165 * @param { MSTensor[] } inputs - indicates the MSTensor array of the inputs. 166 * @returns { Promise<Model> } the promise returned by the function. 167 * @syscap SystemCapability.AI.MindSporeLite 168 * @stagemodelonly 169 * @since 10 170 */ 171 predict(inputs: MSTensor[]): Promise<MSTensor[]>; 172 173 /** 174 * resize model input 175 * @param { MSTensor[] } inputs - indicates the MSTensor array of the inputs. 176 * @param { Array<Array<number>> } dims - indicates the target new shape array 177 * @returns { boolean } the boolean result if the resize operation is successful 178 * @syscap SystemCapability.AI.MindSporeLite 179 * @stagemodelonly 180 * @since 10 181 */ 182 resize(inputs: MSTensor[], dims: Array<Array<number>>): boolean; 183 } 184 185 /** 186 * Provides the device configurations 187 * @typedef Context 188 * @syscap SystemCapability.AI.MindSporeLite 189 * @stagemodelonly 190 * @since 10 191 */ 192 interface Context { 193 /** 194 * The target device 195 * @type {string[]} 196 * @since 10 197 */ 198 target?: string[]; 199 /** 200 * The cpu device information 201 * @type {CpuDevice} 202 * @since 10 203 */ 204 cpu?: CpuDevice; 205 /** 206 * The NNRT device information 207 * @type {NNRTDevice} 208 * @since 10 209 */ 210 nnrt?: NNRTDevice; 211 } 212 213 /** 214 * Provides the CPU device info 215 * @typedef CpuDevice 216 * @syscap SystemCapability.AI.MindSporeLite 217 * @stagemodelonly 218 * @since 10 219 */ 220 interface CpuDevice { 221 /** 222 * The thread num 223 * @type {number} 224 * @since 10 225 */ 226 threadNum?: number; 227 /** 228 * The thread affinity mode 229 * @type {ThreadAffinityMode} 230 * @since 10 231 */ 232 threadAffinityMode?: ThreadAffinityMode; 233 /** 234 * The thread affinity core list 235 * @type {number[]} 236 * @since 10 237 */ 238 threadAffinityCoreList?: number[]; 239 /** 240 * The precision mode 241 * @type {string} 242 * @since 10 243 */ 244 precisionMode?: string; 245 } 246 247 /** 248 * Provides the NNRT device info 249 * @typedef NNRTDevice 250 * @syscap SystemCapability.AI.MindSporeLite 251 * @stagemodelonly 252 * @since 10 253 */ 254 interface NNRTDevice { 255 } 256 257 /** 258 * Enum for provides CPU thread affinity mode 259 * @enum {number} 260 * @syscap SystemCapability.AI.MindSporeLite 261 * @stagemodelonly 262 * @since 10 263 */ 264 export enum ThreadAffinityMode { 265 /** 266 * Thread affinity mode is no bind. 267 * @syscap SystemCapability.AI.MindSporeLite 268 * @since 10 269 */ 270 NO_AFFINITIES = 0, 271 272 /** 273 * Thread affinity mode is big cores first 274 * @syscap SystemCapability.AI.MindSporeLite 275 * @since 10 276 */ 277 BIG_CORES_FIRST = 1, 278 279 /** 280 * Thread affinity mode is little cores first 281 * @syscap SystemCapability.AI.MindSporeLite 282 * @since 10 283 */ 284 LITTLE_CORES_FIRST = 2, 285 } 286 287 /** 288 * Provides MSTensor definition 289 * @typedef MSTensor 290 * @syscap SystemCapability.AI.MindSporeLite 291 * @stagemodelonly 292 * @since 10 293 */ 294 interface MSTensor { 295 /** 296 * The name of the tensor. 297 * @type {string} 298 * @since 10 299 */ 300 name: string; 301 /** 302 * The shape of the tensor. 303 * @type {number[]} 304 * @since 10 305 */ 306 shape: number[]; 307 /** 308 * The number of elements in the tensor. 309 * @type {number} 310 * @since 10 311 */ 312 elementNum: number; 313 /** 314 * The data size of the tensor. 315 * @type {number} 316 * @since 10 317 */ 318 dataSize: number; 319 /** 320 * The data type of the tensor. 321 * @type {DataType} 322 * @since 10 323 */ 324 dtype: DataType; 325 /** 326 * The format of the tensor. 327 * @type {DataType} 328 * @since 10 329 */ 330 format: Format; 331 332 /** 333 * Get MSTensor data 334 * @returns { ArrayBuffer } the data of tensor 335 * @syscap SystemCapability.AI.MindSporeLite 336 * @stagemodelonly 337 * @since 10 338 */ 339 getData(): ArrayBuffer; 340 341 /** 342 * Set MSTensor data 343 * @param { ArrayBuffer } inputArray - indicates the buffer of tensor 344 * @syscap SystemCapability.AI.MindSporeLite 345 * @stagemodelonly 346 * @since 10 347 */ 348 setData(inputArray: ArrayBuffer): void; 349 } 350 351 /** 352 * Enum for provides MSTensor data type 353 * @enum {number} 354 * @syscap SystemCapability.AI.MindSporeLite 355 * @stagemodelonly 356 * @since 10 357 */ 358 export enum DataType { 359 /** 360 * data type is unknown 361 * @syscap SystemCapability.AI.MindSporeLite 362 * @since 10 363 */ 364 TYPE_UNKNOWN = 0, 365 /** 366 * data type is int8 367 * @syscap SystemCapability.AI.MindSporeLite 368 * @since 10 369 */ 370 NUMBER_TYPE_INT8 = 32, 371 /** 372 * data type is int16 373 * @syscap SystemCapability.AI.MindSporeLite 374 * @since 10 375 */ 376 NUMBER_TYPE_INT16 = 33, 377 /** 378 * data type is int32 379 * @syscap SystemCapability.AI.MindSporeLite 380 * @since 10 381 */ 382 NUMBER_TYPE_INT32 = 34, 383 /** 384 * data type is int64 385 * @syscap SystemCapability.AI.MindSporeLite 386 * @since 10 387 */ 388 NUMBER_TYPE_INT64 = 35, 389 /** 390 * data type is uint8 391 * @syscap SystemCapability.AI.MindSporeLite 392 * @since 10 393 */ 394 NUMBER_TYPE_UINT8 = 37, 395 /** 396 * data type is uint16 397 * @syscap SystemCapability.AI.MindSporeLite 398 * @since 10 399 */ 400 NUMBER_TYPE_UINT16 = 38, 401 /** 402 * data type is uint32 403 * @syscap SystemCapability.AI.MindSporeLite 404 * @since 10 405 */ 406 NUMBER_TYPE_UINT32 = 39, 407 /** 408 * data type is uint64 409 * @syscap SystemCapability.AI.MindSporeLite 410 * @since 10 411 */ 412 NUMBER_TYPE_UINT64 = 40, 413 /** 414 * data type is float16 415 * @syscap SystemCapability.AI.MindSporeLite 416 * @since 10 417 */ 418 NUMBER_TYPE_FLOAT16 = 42, 419 /** 420 * data type is float32 421 * @syscap SystemCapability.AI.MindSporeLite 422 * @since 10 423 */ 424 NUMBER_TYPE_FLOAT32 = 43, 425 /** 426 * data type is float64 427 * @syscap SystemCapability.AI.MindSporeLite 428 * @since 10 429 */ 430 NUMBER_TYPE_FLOAT64 = 44, 431 } 432 433 /** 434 * Enum for provides MSTensor format 435 * @enum {number} 436 * @syscap SystemCapability.AI.MindSporeLite 437 * @stagemodelonly 438 * @since 10 439 */ 440 export enum Format { 441 /** 442 * data format is default 443 * @syscap SystemCapability.AI.MindSporeLite 444 * @since 10 445 */ 446 DEFAULT_FORMAT = -1, 447 /** 448 * data format is NCHW 449 * @syscap SystemCapability.AI.MindSporeLite 450 * @since 10 451 */ 452 NCHW = 0, 453 /** 454 * data format is NHWC 455 * @syscap SystemCapability.AI.MindSporeLite 456 * @since 10 457 */ 458 NHWC = 1, 459 /** 460 * data format is NHWC4 461 * @syscap SystemCapability.AI.MindSporeLite 462 * @since 10 463 */ 464 NHWC4 = 2, 465 /** 466 * data format is HWKC 467 * @syscap SystemCapability.AI.MindSporeLite 468 * @since 10 469 */ 470 HWKC = 3, 471 /** 472 * data format is HWCK 473 * @syscap SystemCapability.AI.MindSporeLite 474 * @since 10 475 */ 476 HWCK = 4, 477 /** 478 * data format is KCHW 479 * @syscap SystemCapability.AI.MindSporeLite 480 * @since 10 481 */ 482 KCHW = 5, 483 } 484} 485export default mindSporeLite; 486