1# @ohos.ai.mindSporeLite (端侧AI框架) 2 3MindSpore Lite是一个轻量化、高性能的端侧AI引擎,提供了标准的模型推理和训练接口,内置通用硬件高性能算子库,支持Neural Network Runtime Kit使能AI专用芯片加速推理,助力打造全场景智能应用。 4 5本模块主要介绍MindSpore Lite AI引擎支持模型端侧推理/训练的相关能力。 6 7> **说明:** 8> 9> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。示例代码使用模型均为MindSpore端侧模型。 10> 11> - 本模块接口仅可在Stage模型下使用。 12 13## 导入模块 14 15```ts 16import { mindSporeLite } from '@kit.MindSporeLiteKit'; 17``` 18 19## mindSporeLite.loadModelFromFile 20 21loadModelFromFile(model: string, callback: Callback<Model>): void 22 23从完整路径加载输入模型用于推理。使用callback异步回调。 24 25**系统能力:** SystemCapability.AI.MindSporeLite 26 27**参数:** 28 29| 参数名 | 类型 | 必填 | 说明 | 30| -------- | ------------------------- | ---- | ------------------------ | 31| model | string | 是 | 模型的完整输入路径。 | 32| callback | Callback<[Model](#model)> | 是 | 回调函数。返回模型对象。 | 33 34**示例:** 35 36```ts 37let modelFile : string = '/path/to/xxx.ms'; 38mindSporeLite.loadModelFromFile(modelFile, (mindSporeLiteModel : mindSporeLite.Model) => { 39 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 40 console.info(modelInputs[0].name); 41}) 42``` 43## mindSporeLite.loadModelFromFile 44 45loadModelFromFile(model: string, context: Context, callback: Callback<Model>): void 46 47从完整路径加载输入模型用于推理。使用callback异步回调。 48 49**系统能力:** SystemCapability.AI.MindSporeLite 50 51**参数:** 52 53| 参数名 | 类型 | 必填 | 说明 | 54| -------- | ----------------------------------- | ---- | ---------------------- | 55| model | string | 是 | 模型的完整输入路径。 | 56| context | [Context](#context) | 是 | 运行环境的配置信息。 | 57| callback | Callback<[Model](#model)> | 是 | 回调函数。返回模型对象。 | 58 59**示例:** 60 61```ts 62let context: mindSporeLite.Context = {}; 63context.target = ['cpu']; 64let modelFile : string = '/path/to/xxx.ms'; 65mindSporeLite.loadModelFromFile(modelFile, context, (mindSporeLiteModel : mindSporeLite.Model) => { 66 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 67 console.info(modelInputs[0].name); 68}) 69``` 70## mindSporeLite.loadModelFromFile 71 72loadModelFromFile(model: string, context?: Context): Promise<Model> 73 74从完整路径加载输入模型用于推理。使用Promise异步函数。 75 76**系统能力:** SystemCapability.AI.MindSporeLite 77 78**参数:** 79 80| 参数名 | 类型 | 必填 | 说明 | 81| ------- | ------------------- | ---- | --------------------------------------------- | 82| model | string | 是 | 模型的完整输入路径。 | 83| context | [Context](#context) | 否 | 运行环境的配置信息。默认使用CpuDevice初始化。 | 84 85**返回值:** 86 87| 类型 | 说明 | 88| ------------------------- | ---------------------------- | 89| Promise<[Model](#model)> | Promise对象。返回Model对象。 | 90 91**示例:** 92 93```ts 94let modelFile = '/path/to/xxx.ms'; 95mindSporeLite.loadModelFromFile(modelFile).then((mindSporeLiteModel : mindSporeLite.Model) => { 96 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 97 console.info(modelInputs[0].name); 98}) 99``` 100## mindSporeLite.loadModelFromBuffer 101 102loadModelFromBuffer(model: ArrayBuffer, callback: Callback<Model>): void 103 104从内存加载输入模型用于推理。使用callback异步回调。 105 106**系统能力:** SystemCapability.AI.MindSporeLite 107 108**参数:** 109 110| 参数名 | 类型 | 必填 | 说明 | 111| -------- | ------------------------- | ---- | ------------------------ | 112| model | ArrayBuffer | 是 | 包含模型的内存。 | 113| callback | Callback<[Model](#model)> | 是 | 回调函数。返回模型对象。 | 114 115**示例:** 116 117```ts 118import { mindSporeLite } from '@kit.MindSporeLiteKit'; 119import { common } from '@kit.AbilityKit'; 120import { UIContext } from '@kit.ArkUI'; 121 122let modelFile = 'xxx.ms'; 123let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 124globalContext.getApplicationContext().resourceManager.getRawFileContent(modelFile).then((buffer: Uint8Array) => { 125 let modelBuffer = buffer.buffer; 126 mindSporeLite.loadModelFromBuffer(modelBuffer, (mindSporeLiteModel: mindSporeLite.Model) => { 127 let modelInputs: mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 128 console.info('MS_LITE_LOG: ' + modelInputs[0].name); 129 }) 130}) 131``` 132## mindSporeLite.loadModelFromBuffer 133 134loadModelFromBuffer(model: ArrayBuffer, context: Context, callback: Callback<Model>): void 135 136从内存加载输入模型用于推理。使用callback异步回调。 137 138**系统能力:** SystemCapability.AI.MindSporeLite 139 140**参数:** 141 142| 参数名 | 类型 | 必填 | 说明 | 143| -------- | ----------------------------------- | ---- | ---------------------- | 144| model | ArrayBuffer | 是 | 包含模型的内存。 | 145| context | [Context](#context) | 是 | 运行环境的配置信息。 | 146| callback | Callback<[Model](#model)> | 是 | 回调函数。返回模型对象。 | 147 148**示例:** 149 150```ts 151import { mindSporeLite } from '@kit.MindSporeLiteKit'; 152import { common } from '@kit.AbilityKit'; 153import { UIContext } from '@kit.ArkUI'; 154 155let modelFile = 'xxx.ms'; 156let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 157globalContext.getApplicationContext().resourceManager.getRawFileContent(modelFile).then((buffer: Uint8Array) => { 158 let modelBuffer = buffer.buffer; 159 let context: mindSporeLite.Context = {}; 160 context.target = ['cpu']; 161 mindSporeLite.loadModelFromBuffer(modelBuffer, context, (mindSporeLiteModel: mindSporeLite.Model) => { 162 let modelInputs: mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 163 console.info('MS_LITE_LOG: ' + modelInputs[0].name); 164 }) 165}) 166``` 167## mindSporeLite.loadModelFromBuffer 168 169loadModelFromBuffer(model: ArrayBuffer, context?: Context): Promise<Model> 170 171从内存加载输入模型用于推理。使用Promise异步函数。 172 173**系统能力:** SystemCapability.AI.MindSporeLite 174 175**参数:** 176 177| 参数名 | 类型 | 必填 | 说明 | 178| ------- | ------------------- | ---- | --------------------------------------------- | 179| model | ArrayBuffer | 是 | 包含模型的内存。 | 180| context | [Context](#context) | 否 | 运行环境的配置信息。默认使用CpuDevice初始化。 | 181 182**返回值:** 183 184| 类型 | 说明 | 185| ------------------------------- | ---------------------------- | 186| Promise<[Model](#model)> | Promise对象。返回Model对象。 | 187 188**示例:** 189 190```ts 191import { mindSporeLite } from '@kit.MindSporeLiteKit'; 192import { common } from '@kit.AbilityKit'; 193import { UIContext } from '@kit.ArkUI'; 194 195let modelFile = 'xxx.ms'; 196let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 197globalContext.getApplicationContext().resourceManager.getRawFileContent(modelFile).then((buffer: Uint8Array) => { 198 let modelBuffer = buffer.buffer; 199 mindSporeLite.loadModelFromBuffer(modelBuffer).then((mindSporeLiteModel: mindSporeLite.Model) => { 200 let modelInputs: mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 201 console.info('MS_LITE_LOG: ' + modelInputs[0].name); 202 }) 203}) 204``` 205## mindSporeLite.loadModelFromFd 206 207loadModelFromFd(model: number, callback: Callback<Model>): void 208 209从文件描述符加载输入模型用于推理。使用callback异步回调。 210 211**系统能力:** SystemCapability.AI.MindSporeLite 212 213**参数:** 214 215| 参数名 | 类型 | 必填 | 说明 | 216| -------- | ----------------------------------- | ---- | ---------------------- | 217| model | number | 是 | 模型的文件描述符。 | 218| callback | Callback<[Model](#model)> | 是 | 回调函数。返回模型对象。 | 219 220**示例:** 221 222```ts 223import { fileIo } from '@kit.CoreFileKit'; 224let modelFile = '/path/to/xxx.ms'; 225let file = fileIo.openSync(modelFile, fileIo.OpenMode.READ_ONLY); 226mindSporeLite.loadModelFromFd(file.fd, (mindSporeLiteModel : mindSporeLite.Model) => { 227 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 228 console.info(modelInputs[0].name); 229}) 230``` 231## mindSporeLite.loadModelFromFd 232 233loadModelFromFd(model: number, context: Context, callback: Callback<Model>): void 234 235从文件描述符加载输入模型用于推理。使用callback异步回调。 236 237**系统能力:** SystemCapability.AI.MindSporeLite 238 239**参数:** 240 241| 参数名 | 类型 | 必填 | 说明 | 242| -------- | ----------------------------------- | ---- | ---------------------- | 243| model | number | 是 | 模型的文件描述符。 | 244| context | [Context](#context) | 是 | 运行环境的配置信息。 | 245| callback | Callback<[Model](#model)> | 是 | 回调函数。返回模型对象。 | 246 247**示例:** 248 249```ts 250import { fileIo } from '@kit.CoreFileKit'; 251let modelFile = '/path/to/xxx.ms'; 252let context : mindSporeLite.Context = {}; 253context.target = ['cpu']; 254let file = fileIo.openSync(modelFile, fileIo.OpenMode.READ_ONLY); 255mindSporeLite.loadModelFromFd(file.fd, context, (mindSporeLiteModel : mindSporeLite.Model) => { 256 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 257 console.info(modelInputs[0].name); 258}) 259``` 260## mindSporeLite.loadModelFromFd 261 262loadModelFromFd(model: number, context?: Context): Promise<Model> 263 264从文件描述符加载输入模型用于推理。使用Promise异步函数。 265 266**系统能力:** SystemCapability.AI.MindSporeLite 267 268**参数:** 269 270| 参数名 | 类型 | 必填 | 说明 | 271| ------- | ------------------- | ---- | --------------------------------------------- | 272| model | number | 是 | 模型的文件描述符。 | 273| context | [Context](#context) | 否 | 运行环境的配置信息。默认使用CpuDevice初始化。 | 274 275**返回值:** 276 277| 类型 | 说明 | 278| ------------------------- | ---------------------------- | 279| Promise<[Model](#model)> | Promise对象。返回Model对象。 | 280 281**示例:** 282 283```ts 284import { fileIo } from '@kit.CoreFileKit'; 285let modelFile = '/path/to/xxx.ms'; 286let file = fileIo.openSync(modelFile, fileIo.OpenMode.READ_ONLY); 287mindSporeLite.loadModelFromFd(file.fd).then((mindSporeLiteModel: mindSporeLite.Model) => { 288 let modelInputs: mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 289 console.info(modelInputs[0].name); 290}) 291``` 292 293## mindSporeLite.loadTrainModelFromFile<sup>12+</sup> 294 295loadTrainModelFromFile(model: string, trainCfg?: TrainCfg, context?: Context): Promise<Model> 296 297根据路径加载训练模型文件。使用Promise异步回调。 298 299**系统能力:** SystemCapability.AI.MindSporeLite 300 301**参数:** 302 303| 参数名 | 类型 | 必填 | 说明 | 304| -------- | ----------------------- | ---- | ---------------------------------------------- | 305| model | string | 是 | 模型的完整输入路径。 | 306| trainCfg | [TrainCfg](#traincfg12) | 否 | 模型训练配置。默认值为TrainCfg各属性默认值。 | 307| context | [Context](#context) | 否 | 运行环境的配置信息。默认使用CpuDevice初始化。 | 308 309**返回值:** 310 311| 类型 | 说明 | 312| ------------------------ | -------------------- | 313| Promise<[Model](#model)> | Promise对象。返回Model对象。 | 314 315**示例:** 316 317```ts 318let modelFile = '/path/to/xxx.ms'; 319mindSporeLite.loadTrainModelFromFile(modelFile).then((mindSporeLiteModel : mindSporeLite.Model) => { 320 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 321 console.info(modelInputs[0].name); 322}) 323``` 324 325## mindSporeLite.loadTrainModelFromBuffer<sup>12+</sup> 326 327loadTrainModelFromBuffer(model: ArrayBuffer, trainCfg?: TrainCfg, context?: Context): Promise<Model> 328 329从内存缓冲区加载训练模型。使用Promise异步回调。 330 331**系统能力:** SystemCapability.AI.MindSporeLite 332 333**参数:** 334 335| 参数名 | 类型 | 必填 | 说明 | 336| -------- | ----------------------- | ---- | --------------------------------------------- | 337| model | ArrayBuffer | 是 | 包含训练模型的内存。 | 338| trainCfg | [TrainCfg](#traincfg12) | 否 | 模型训练配置。默认值为TrainCfg各属性默认值。 | 339| context | [Context](#context) | 否 | 运行环境的配置信息。默认使用CpuDevice初始化。 | 340 341**返回值:** 342 343| 类型 | 说明 | 344| ------------------------ | -------------------- | 345| Promise<[Model](#model)> | Promise对象。返回Model对象。 | 346 347**示例:** 348 349```ts 350import { mindSporeLite } from '@kit.MindSporeLiteKit'; 351import { common } from '@kit.AbilityKit'; 352import { UIContext } from '@kit.ArkUI'; 353 354let modelFile = 'xxx.ms'; 355let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 356globalContext.getApplicationContext().resourceManager.getRawFileContent(modelFile).then((buffer: Uint8Array) => { 357 let modelBuffer = buffer.buffer; 358 mindSporeLite.loadTrainModelFromBuffer(modelBuffer).then((mindSporeLiteModel: mindSporeLite.Model) => { 359 console.info("MSLITE trainMode: ", mindSporeLiteModel.trainMode); 360 }) 361}) 362``` 363 364## mindSporeLite.loadTrainModelFromFd<sup>12+</sup> 365 366loadTrainModelFromFd(model: number, trainCfg?: TrainCfg, context?: Context): Promise<Model> 367 368从文件描述符加载训练模型文件。使用Promise异步回调。 369 370**系统能力:** SystemCapability.AI.MindSporeLite 371 372**参数:** 373 374| 参数名 | 类型 | 必填 | 说明 | 375| -------- | ----------------------- | ---- | --------------------------------------------- | 376| model | number | 是 | 训练模型的文件描述符。 | 377| trainCfg | [TrainCfg](#traincfg12) | 否 | 模型训练配置。默认值为TrainCfg各属性默认值。 | 378| context | [Context](#context) | 否 | 运行环境的配置信息。默认使用CpuDevice初始化。 | 379 380**返回值:** 381 382| 类型 | 说明 | 383| ------------------------ | ---------------------------- | 384| Promise<[Model](#model)> | Promise对象。返回Model对象。 | 385 386**示例:** 387 388```ts 389import { fileIo } from '@kit.CoreFileKit'; 390let modelFile = '/path/to/xxx.ms'; 391let file = fileIo.openSync(modelFile, fileIo.OpenMode.READ_ONLY); 392mindSporeLite.loadTrainModelFromFd(file.fd).then((mindSporeLiteModel: mindSporeLite.Model) => { 393 console.info("MSLITE trainMode: ", mindSporeLiteModel.trainMode); 394}); 395``` 396 397## mindSporeLite.getAllNNRTDeviceDescriptions<sup>12+</sup> 398 399getAllNNRTDeviceDescriptions() : NNRTDeviceDescription[] 400 401获取NNRt中的所有设备描述。 402 403**系统能力:** SystemCapability.AI.MindSporeLite 404 405**返回值:** 406 407| 类型 | 说明 | 408| --------------------------------------------------- | ---------------------- | 409| [NNRTDeviceDescription](#nnrtdevicedescription12)[] | NNRt设备描述信息数组。 | 410 411**示例:** 412 413```ts 414let allDevices = mindSporeLite.getAllNNRTDeviceDescriptions(); 415if (allDevices == null) { 416 console.error('MS_LITE_LOG: getAllNNRTDeviceDescriptions is NULL.'); 417} 418``` 419 420## Context 421 422定义运行环境的配置信息。 423 424### 属性 425 426**系统能力:** SystemCapability.AI.MindSporeLite 427 428 429| 名称 | 类型 | 只读 | 可选 | 说明 | 430| ------ | ------------------------- | ---- | ---- | ------------------------------------------------------------ | 431| target | string[] | 否 | 是 | 配置目标后端。可选'cpu','nnrt',默认'cpu'。 | 432| cpu | [CpuDevice](#cpudevice) | 否 | 是 | CPU后端设备选项。只有当target包含'cpu'时,才能设置此属性。默认值为CpuDevice各属性默认值。 | 433| nnrt | [NNRTDevice](#nnrtdevice) | 否 | 是 | NNRt后端设备选项。只有当target包含'nnrt'时,才能设置此属性。默认值为NNRTDevice各属性默认值。 | 434 435**示例:** 436 437```ts 438let context: mindSporeLite.Context = {}; 439context.target = ['cpu','nnrt']; 440``` 441 442## CpuDevice 443 444CPU后端设备选项。 445 446### 属性 447 448**系统能力:** SystemCapability.AI.MindSporeLite 449 450| 名称 | 类型 | 只读 | 可选 | 说明 | 451| ---------------------- | ----------------------------------------- | ---- | ---- | ------------------------------------------------------------ | 452| threadNum | number | 否 | 是 | 设置运行时的线程数,默认值:2。 | 453| threadAffinityMode | [ThreadAffinityMode](#threadaffinitymode) | 否 | 是 | 设置运行时的CPU绑核策略模式,默认值为不绑核:mindSporeLite.ThreadAffinityMode.NO_AFFINITIES。 | 454| threadAffinityCoreList | number[] | 否 | 是 | 设置运行时的CPU绑核列表,设置绑核策略模式后使能,当绑核策略模式为mindSporeLite.ThreadAffinityMode.NO_AFFINITIES时,绑核列表为空。列表中的数字代表核的序号。默认值:[]。 | 455| precisionMode | string | 否 | 是 | 设置是否使能**Float16推理模式**,设置为'preferred_fp16'代表使能半精度推理,其余设置情况均为不支持,默认设置'enforce_fp32'表示不使能半精度推理。 | 456 457**Float16推理模式**: Float16又称半精度,它使用16比特表示一个数。Float16推理模式表示推理的时候用半精度进行推理。 458 459**示例:** 460 461```ts 462let context: mindSporeLite.Context = {}; 463context.cpu = {}; 464context.target = ['cpu']; 465context.cpu.threadNum = 2; 466context.cpu.threadAffinityMode = 0; 467context.cpu.precisionMode = 'preferred_fp16'; 468context.cpu.threadAffinityCoreList = [0, 1, 2]; 469``` 470 471## ThreadAffinityMode 472 473设置运行时的CPU绑核策略模式,有效值为0-2,0为默认不绑核,1为绑大核,2为绑中核。 474 475**系统能力:** SystemCapability.AI.MindSporeLite 476 477| 名称 | 值 | 说明 | 478| ------------------ | ---- | ------------ | 479| NO_AFFINITIES | 0 | 不绑核。 | 480| BIG_CORES_FIRST | 1 | 绑大核优先。 | 481| LITTLE_CORES_FIRST | 2 | 绑中核优先。 | 482 483## NNRTDevice 484 485Neural Network Runtime表示神经网络运行时,简称NNRt。作为中间桥梁,连通上层 AI 推理框架和底层加速芯片,实现 AI 模型的跨芯片推理计算。MindSpore Lite 可配置NNRt后端。 486 487### 属性 488 489**系统能力:** SystemCapability.AI.MindSporeLite 490 491| 名称 | 类型 | 只读 | 可选 | 说明 | 492| ----------------------------- | ----------------------------------- | ---- | ------------------------ | ------------------------ | 493| deviceID<sup>12+</sup> | bigint | 否 | 是 | NNRt设备ID。默认值为0。 | 494| performanceMode<sup>12+</sup> | [PerformanceMode](#performancemode12) | 否 | 是 | NNRt设备的工作性能模式。默认值为PERFORMANCE_NONE。 | 495| priority<sup>12+</sup> | [Priority](#priority12) | 否 | 是 | NNRt推理任务优先级。默认值为PRIORITY_MEDIUM。 | 496| extensions<sup>12+</sup> | [Extension](#extension12)[] | 否 | 是 | NNRt设备的扩展配置。默认为空。 | 497 498## PerformanceMode<sup>12+</sup> 499 500NNRt设备的工作性能模式枚举。 501 502**系统能力:** SystemCapability.AI.MindSporeLite 503 504| 名称 | 值 | 说明 | 505| ------------------- | ---- | ------------------- | 506| PERFORMANCE_NONE | 0 | 无特殊设置。 | 507| PERFORMANCE_LOW | 1 | 低功耗模式。 | 508| PERFORMANCE_MEDIUM | 2 | 功耗-性能均衡模式。 | 509| PERFORMANCE_HIGH | 3 | 高性能模式。 | 510| PERFORMANCE_EXTREME | 4 | 极致性能模式。 | 511 512## Priority<sup>12+</sup> 513 514NNRt推理任务优先级枚举。 515 516**系统能力:** SystemCapability.AI.MindSporeLite 517 518| 名称 | 值 | 说明 | 519| --------------- | ---- | -------------- | 520| PRIORITY_NONE | 0 | 无优先级偏好。 | 521| PRIORITY_LOW | 1 | 低优先级任务。 | 522| PRIORITY_MEDIUM | 2 | 中优先级任务。 | 523| PRIORITY_HIGH | 3 | 高优先级任务。 | 524 525## Extension<sup>12+</sup> 526 527定义NNRt设备的扩展信息。 528 529### 属性 530 531**系统能力:** SystemCapability.AI.MindSporeLite 532 533| 名称 | 类型 | 只读 | 可选 | 说明 | 534| ------------------- | ----------- | ---- | ---- | ---------------- | 535| name<sup>12+</sup> | string | 否 | 否 | 扩展名称。 | 536| value<sup>12+</sup> | ArrayBuffer | 否 | 否 | 包含扩展的内存。 | 537 538## NNRTDeviceDescription<sup>12+</sup> 539 540NNRt设备信息描述,包含设备ID,设备名称等信息。 541 542**系统能力:** SystemCapability.AI.MindSporeLite 543 544### deviceID 545 546deviceID() : bigint 547 548获取NNRt设备ID。 549 550**系统能力:** SystemCapability.AI.MindSporeLite 551 552**返回值:** 553 554| 类型 | 说明 | 555| ------ | ------------ | 556| bigint | NNRt设备ID。 | 557 558**示例:** 559 560```ts 561let allDevices = mindSporeLite.getAllNNRTDeviceDescriptions(); 562if (allDevices == null) { 563 console.error('getAllNNRTDeviceDescriptions is NULL.'); 564} 565let context: mindSporeLite.Context = {}; 566context.target = ["nnrt"]; 567context.nnrt = {}; 568for (let i: number = 0; i < allDevices.length; i++) { 569 console.info(allDevices[i].deviceID().toString()); 570} 571``` 572 573### deviceType 574 575deviceType() : NNRTDeviceType 576 577获取NNRt设备类型。 578 579**系统能力:** SystemCapability.AI.MindSporeLite 580 581**返回值:** 582 583| 类型 | 说明 | 584| ----------------------------------- | -------------- | 585| [NNRTDeviceType](#nnrtdevicetype12) | NNRt设备类型。 | 586 587**示例:** 588 589```ts 590let allDevices = mindSporeLite.getAllNNRTDeviceDescriptions(); 591if (allDevices == null) { 592 console.error('getAllNNRTDeviceDescriptions is NULL.'); 593} 594let context: mindSporeLite.Context = {}; 595context.target = ["nnrt"]; 596context.nnrt = {}; 597for (let i: number = 0; i < allDevices.length; i++) { 598 console.info(allDevices[i].deviceType().toString()); 599} 600``` 601 602### deviceName 603 604deviceName() : string 605 606获取NNRt设备名称。 607 608**系统能力:** SystemCapability.AI.MindSporeLite 609 610**返回值:** 611 612| 类型 | 说明 | 613| ------ | -------------- | 614| string | NNRt设备名称。 | 615 616**示例:** 617 618```ts 619let allDevices = mindSporeLite.getAllNNRTDeviceDescriptions(); 620if (allDevices == null) { 621 console.error('getAllNNRTDeviceDescriptions is NULL.'); 622} 623let context: mindSporeLite.Context = {}; 624context.target = ["nnrt"]; 625context.nnrt = {}; 626for (let i: number = 0; i < allDevices.length; i++) { 627 console.info(allDevices[i].deviceName().toString()); 628} 629``` 630 631## NNRTDeviceType<sup>12+</sup> 632 633NNRt设备类型枚举。 634 635**系统能力:** SystemCapability.AI.MindSporeLite 636 637| 名称 | 值 | 说明 | 638| ---------------------- | ---- | ----------------------------------- | 639| NNRTDEVICE_OTHERS | 0 | 设备类型不属于以下3种,则属于其它。 | 640| NNRTDEVICE_CPU | 1 | CPU设备。 | 641| NNRTDEVICE_GPU | 2 | GPU设备。 | 642| NNRTDEVICE_ACCELERATOR | 3 | 特定的加速设备。 | 643 644## TrainCfg<sup>12+</sup> 645 646端侧训练相关参数的配置文件。 647 648### 属性 649 650**系统能力:** SystemCapability.AI.MindSporeLite 651 652| 名称 | 类型 | 只读 | 可选 | 说明 | 653| ------------------------------- | ----------------------------------------- | ---- | ---- | ------------------------------------------------------------ | 654| lossName<sup>12+</sup> | string[] | 否 | 是 | 损失函数的名称列表。默认值为["loss_fct", "_loss_fn", "SigmoidCrossEntropy"]。 | 655| optimizationLevel<sup>12+</sup> | [OptimizationLevel](#optimizationlevel12) | 否 | 是 | 端侧训练的网络优化等级。默认值为O0。 | 656 657**示例:** 658 659```ts 660let cfg: mindSporeLite.TrainCfg = {}; 661cfg.lossName = ["loss_fct", "_loss_fn", "SigmoidCrossEntropy"]; 662cfg.optimizationLevel = mindSporeLite.OptimizationLevel.O0; 663``` 664 665## OptimizationLevel<sup>12+</sup> 666 667端侧训练的网络优化等级枚举。 668 669**系统能力:** SystemCapability.AI.MindSporeLite 670 671| 名称 | 值 | 说明 | 672| ---- | ---- | ---------------------------------------------------------- | 673| O0 | 0 | 无优化等级。 | 674| O2 | 2 | 将网络转换为float16,保持批量归一化层和损失函数为float32。 | 675| O3 | 3 | 将网络转换为float16,包括批量归一化层。 | 676| AUTO | 4 | 根据设备选择优化等级。 | 677 678 679## QuantizationType<sup>12+</sup> 680 681量化类型信息,有效值为0-2。 682 683**系统能力:** SystemCapability.AI.MindSporeLite 684 685| 名称 | 值 | 说明 | 686| ------------ | ---- | ---------- | 687| NO_QUANT | 0 | 不做量化。 | 688| WEIGHT_QUANT | 1 | 权重量化。 | 689| FULL_QUANT | 2 | 全量化。 | 690 691## Model 692 693模型实例。描述Model对象的属性和方法。 694 695下例API示例中都需先使用[loadModelFromFile()](#mindsporeliteloadmodelfromfile)、[loadModelFromBuffer()](#mindsporeliteloadmodelfrombuffer)、[loadModelFromFd()](#mindsporeliteloadmodelfromfd)中的任一方法获取到Model实例,再通过此实例调用对应方法。 696 697### 属性 698 699**系统能力:** SystemCapability.AI.MindSporeLite 700 701| 名称 | 类型 | 只读 | 可选 | 说明 | 702| -------------------------- | ------- | ---- | ---- | ------------------------------------------------------------ | 703| learningRate<sup>12+</sup> | number | 否 | 是 | 训练模型的学习率。默认值从加载的模型中读取。 | 704| trainMode<sup>12+</sup> | boolean | 否 | 是 | 模型是否为训练模式。true表示训练模式,false表示非训练模式。如果是训练模型,trainMode默认是true;如果是推理模型,trainMode默认是false。 | 705 706### getInputs 707 708getInputs(): MSTensor[] 709 710获取模型的输入用于推理。 711 712**系统能力:** SystemCapability.AI.MindSporeLite 713 714**返回值:** 715 716| 类型 | 说明 | 717| ----------------------- | ------------------ | 718| [MSTensor](#mstensor)[] | 返回MSTensor对象。 | 719 720**示例:** 721 722```ts 723let modelFile = '/path/to/xxx.ms'; 724mindSporeLite.loadModelFromFile(modelFile).then((mindSporeLiteModel : mindSporeLite.Model) => { 725 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 726 console.info(modelInputs[0].name); 727}) 728``` 729### predict 730 731predict(inputs: MSTensor[], callback: Callback<MSTensor[]>): void 732 733执行推理模型。使用callback异步回调。需要确保调用时模型对象不被资源回收。 734 735**系统能力:** SystemCapability.AI.MindSporeLite 736 737**参数:** 738 739| 参数名 | 类型 | 必填 | 说明 | 740| ------ | ----------------------- | ---- | -------------------------- | 741| inputs | [MSTensor](#mstensor)[] | 是 | 模型的输入列表。MSTensor对象。 | 742| callback | Callback<[MSTensor](#mstensor)[]> | 是 | 回调函数。返回MSTensor对象。 | 743 744**示例:** 745 746```ts 747import { mindSporeLite } from '@kit.MindSporeLiteKit'; 748import { common } from '@kit.AbilityKit'; 749import { UIContext } from '@kit.ArkUI'; 750 751let inputName = 'input_data.bin'; 752let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 753globalContext.getApplicationContext().resourceManager.getRawFileContent(inputName).then(async (buffer : Uint8Array) => { 754 let inputBuffer = buffer.buffer; 755 let modelFile : string = '/path/to/xxx.ms'; 756 let mindSporeLiteModel : mindSporeLite.Model = await mindSporeLite.loadModelFromFile(modelFile); 757 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 758 759 modelInputs[0].setData(inputBuffer); 760 mindSporeLiteModel.predict(modelInputs, (mindSporeLiteTensor : mindSporeLite.MSTensor[]) => { 761 let output = new Float32Array(mindSporeLiteTensor[0].getData()); 762 for (let i = 0; i < output.length; i++) { 763 console.info('MS_LITE_LOG: ' + output[i].toString()); 764 } 765 }) 766}) 767``` 768### predict 769 770predict(inputs: MSTensor[]): Promise<MSTensor[]> 771 772执行推理模型,返回推理结果。使用Promise异步回调。需要确保调用时模型对象不被资源回收。 773 774**系统能力:** SystemCapability.AI.MindSporeLite 775 776**参数:** 777 778| 参数名 | 类型 | 必填 | 说明 | 779| ------ | ----------------------- | ---- | ------------------------------ | 780| inputs | [MSTensor](#mstensor)[] | 是 | 模型的输入列表。MSTensor对象。 | 781 782**返回值:** 783 784| 类型 | 说明 | 785| ----------------------- | ---------------------- | 786| Promise<[MSTensor](#mstensor)[]> | Promise对象。返回MSTensor对象列表。 | 787 788**示例:** 789 790```ts 791import { mindSporeLite } from '@kit.MindSporeLiteKit'; 792import { common } from '@kit.AbilityKit'; 793import { UIContext } from '@kit.ArkUI'; 794 795let inputName = 'input_data.bin'; 796let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 797globalContext.getApplicationContext().resourceManager.getRawFileContent(inputName).then(async (buffer : Uint8Array) => { 798 let inputBuffer = buffer.buffer; 799 let modelFile = '/path/to/xxx.ms'; 800 let mindSporeLiteModel : mindSporeLite.Model = await mindSporeLite.loadModelFromFile(modelFile); 801 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 802 modelInputs[0].setData(inputBuffer); 803 mindSporeLiteModel.predict(modelInputs).then((mindSporeLiteTensor : mindSporeLite.MSTensor[]) => { 804 let output = new Float32Array(mindSporeLiteTensor[0].getData()); 805 for (let i = 0; i < output.length; i++) { 806 console.info(output[i].toString()); 807 } 808 }) 809}) 810``` 811 812### resize 813 814resize(inputs: MSTensor[], dims: Array<Array<number>>): boolean 815 816重新设置张量大小。 817 818**系统能力:** SystemCapability.AI.MindSporeLite 819 820**参数:** 821 822| 参数名 | 类型 | 必填 | 说明 | 823| ------ | --------------------- | ---- | ----------------------------- | 824| inputs | [MSTensor](#mstensor)[] | 是 | 模型的输入列表。 | 825| dims | Array<Array<number>> | 是 | 需要修改的目标张量大小。 | 826 827**返回值:** 828 829| 类型 | 说明 | 830| ------- | ------------------------------------------------------------ | 831| boolean | 返回是否设置成功的结果。true表示重新设置张量大小成功;false表示重新设置张量大小失败。 | 832 833**示例:** 834 835```ts 836let modelFile = '/path/to/xxx.ms'; 837mindSporeLite.loadModelFromFile(modelFile).then((mindSporeLiteModel : mindSporeLite.Model) => { 838 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 839 let new_dim = new Array([1,32,32,1]); 840 mindSporeLiteModel.resize(modelInputs, new_dim); 841}) 842``` 843 844### runStep<sup>12+</sup> 845 846runStep(inputs: MSTensor[]): boolean 847 848单步训练模型,仅用于端侧训练。 849 850**系统能力:** SystemCapability.AI.MindSporeLite 851 852**参数:** 853 854| 参数名 | 类型 | 必填 | 说明 | 855| ------ | ----------------------- | --- | -------- | 856| inputs | [MSTensor](#mstensor)[] | 是 | 模型的输入列表。 | 857 858**返回值:** 859 860| 类型 | 说明 | 861| ------- | ------------------------------------------------------------ | 862| boolean | 返回单步训练模型是否成功的结果。true表示单步训练模型成功,false表示单步训练模型失败。 | 863 864**示例:** 865 866```ts 867let modelFile = '/path/to/xxx.ms'; 868mindSporeLite.loadTrainModelFromFile(modelFile).then((mindSporeLiteModel: mindSporeLite.Model) => { 869 mindSporeLiteModel.trainMode = true; 870 const modelInputs = mindSporeLiteModel.getInputs(); 871 let ret = mindSporeLiteModel.runStep(modelInputs); 872 if (ret == false) { 873 console.error('MS_LITE_LOG: runStep failed.') 874 } 875}) 876``` 877 878### getWeights<sup>12+</sup> 879 880getWeights(): MSTensor[] 881 882获取模型的所有权重,仅用于端侧训练。 883 884**系统能力:** SystemCapability.AI.MindSporeLite 885 886**返回值:** 887 888| 类型 | 说明 | 889| ----------------------- | ---------- | 890| [MSTensor](#mstensor)[] | 返回模型的权重张量。 | 891 892**示例:** 893 894```ts 895import { mindSporeLite } from '@kit.MindSporeLiteKit'; 896import { common } from '@kit.AbilityKit'; 897import { UIContext } from '@kit.ArkUI'; 898 899let modelFile = 'xxx.ms'; 900let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 901globalContext.getApplicationContext().resourceManager.getRawFileContent(modelFile).then((modelBuffer : Uint8Array) => { 902 mindSporeLite.loadTrainModelFromBuffer(modelBuffer.buffer.slice(0)).then((mindSporeLiteModel: mindSporeLite.Model) => { 903 mindSporeLiteModel.trainMode = true; 904 const weights = mindSporeLiteModel.getWeights(); 905 for (let i = 0; i < weights.length; i++) { 906 let printStr = weights[i].name + ", "; 907 printStr += weights[i].shape + ", "; 908 printStr += weights[i].dtype + ", "; 909 printStr += weights[i].dataSize + ", "; 910 printStr += weights[i].getData(); 911 console.info("MS_LITE weights: ", printStr); 912 } 913 }) 914}) 915``` 916 917### updateWeights<sup>12+</sup> 918 919updateWeights(weights: MSTensor[]): boolean 920 921更新模型的权重,仅用于端侧训练。 922 923**系统能力:** SystemCapability.AI.MindSporeLite 924 925**参数:** 926 927| 参数名 | 类型 | 必填 | 说明 | 928| ------- | ----------------------- | ---- | -------------- | 929| weights | [MSTensor](#mstensor)[] | 是 | 权重张量列表。 | 930 931**返回值:** 932 933| 类型 | 说明 | 934| ------- | ------------------------------------------------------------ | 935| boolean | 返回是否更新权重成功的结果。true表示更新权重成功,false表示更新权重失败。 | 936 937**示例:** 938 939```ts 940import { mindSporeLite } from '@kit.MindSporeLiteKit'; 941import { common } from '@kit.AbilityKit'; 942import { UIContext } from '@kit.ArkUI'; 943 944let modelFile = 'xxx.ms'; 945let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 946globalContext.getApplicationContext().resourceManager.getRawFileContent(modelFile).then((modelBuffer : Uint8Array) => { 947 mindSporeLite.loadTrainModelFromBuffer(modelBuffer.buffer.slice(0)).then((mindSporeLiteModel: mindSporeLite.Model) => { 948 mindSporeLiteModel.trainMode = true; 949 const weights = mindSporeLiteModel.getWeights(); 950 let ret = mindSporeLiteModel.updateWeights(weights); 951 if (ret == false) { 952 console.error('MS_LITE_LOG: updateWeights failed.') 953 } 954 }) 955}) 956``` 957 958### setupVirtualBatch<sup>12+</sup> 959 960setupVirtualBatch(virtualBatchMultiplier: number, lr: number, momentum: number): boolean 961 962设置虚拟批次用于训练,仅用于端侧训练。 963 964**系统能力:** SystemCapability.AI.MindSporeLite 965 966**参数:** 967 968| 参数名 | 类型 | 必填 | 说明 | 969| ---------------------- | ------ | ---- | ---------------------------------------------------- | 970| virtualBatchMultiplier | number | 是 | 虚拟批次乘法器,当设置值小于1时,表示禁用虚拟batch。 | 971| lr | number | 是 | 学习率。 | 972| momentum | number | 是 | 动量。 | 973 974**返回值:** 975 976| 类型 | 说明 | 977| ------- | ------------------------------------------------------------ | 978| boolean | 返回是否设置虚拟批次成功的结果。true表示设置虚拟批次成功,false表示设置虚拟批次失败。 | 979 980**示例:** 981 982```ts 983import { mindSporeLite } from '@kit.MindSporeLiteKit'; 984import { common } from '@kit.AbilityKit'; 985import { UIContext } from '@kit.ArkUI'; 986 987let modelFile = 'xxx.ms'; 988let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 989globalContext.getApplicationContext().resourceManager.getRawFileContent(modelFile).then((modelBuffer : Uint8Array) => { 990 mindSporeLite.loadTrainModelFromBuffer(modelBuffer.buffer.slice(0)).then((mindSporeLiteModel: mindSporeLite.Model) => { 991 mindSporeLiteModel.trainMode = true; 992 let ret = mindSporeLiteModel.setupVirtualBatch(2,-1,-1); 993 if (ret == false) { 994 console.error('MS_LITE setupVirtualBatch failed.') 995 } 996 }) 997}) 998``` 999 1000### exportModel<sup>12+</sup> 1001 1002exportModel(modelFile: string, quantizationType?: QuantizationType, exportInferenceOnly?: boolean, outputTensorName?: string[]): boolean 1003 1004导出训练模型,仅用于端侧训练。 1005 1006**系统能力:** SystemCapability.AI.MindSporeLite 1007 1008**参数:** 1009 1010| 参数名 | 类型 | 必填 | 说明 | 1011| ------------------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 1012| modelFile | string | 是 | 导出模型的文件路径。 | 1013| quantizationType | [QuantizationType](#quantizationtype12) | 否 | 量化类型,默认为NO_QUANT。 | 1014| exportInferenceOnly | boolean | 否 | 是否只导出推理模型。true表示只导出推理模型,false表示导出训练和推理两个模型。默认为true。 | 1015| outputTensorName | string[] | 否 | 设置导出模型的输出张量的名称。默认为空字符串数组,表示全量导出。 | 1016 1017**返回值:** 1018 1019| 类型 | 说明 | 1020| ------- | ------------------------------------------------------------ | 1021| boolean | 返回是否导出训练模型成功的结果。true表示导出训练模型成功,false表示导出训练模型失败。 | 1022 1023**示例:** 1024 1025```ts 1026let modelFile = '/path/to/xxx.ms'; 1027let newPath = '/newpath/to'; 1028mindSporeLite.loadTrainModelFromFile(modelFile).then((mindSporeLiteModel: mindSporeLite.Model) => { 1029 mindSporeLiteModel.trainMode = true; 1030 let ret = mindSporeLiteModel.exportModel(newPath + "/new_model.ms", mindSporeLite.QuantizationType.NO_QUANT, true); 1031 if (ret == false) { 1032 console.error('MS_LITE exportModel failed.') 1033 } 1034}) 1035``` 1036 1037 1038### exportWeightsCollaborateWithMicro<sup>12+</sup> 1039 1040exportWeightsCollaborateWithMicro(weightFile: string, isInference?: boolean, enableFp16?: boolean, changeableWeightsName?: string[]): boolean 1041 1042导出供**micro推理**使用的模型权重,仅用于端侧训练。 1043 1044**micro推理**:MindSpore Lite针对MCUs(MicroControllerUnits)部署硬件后端,提供了一种超轻量Micro AI部署解决方案,离线阶段直接将模型生成轻量化代码,不再需要在线解析模型和图编译。 1045 1046**系统能力:** SystemCapability.AI.MindSporeLite 1047 1048**参数:** 1049 1050| 参数名 | 类型 | 必填 | 说明 | 1051| --------------------- | -------- | ---- | ------------------------------------------------------------ | 1052| weightFile | string | 是 | 权重文件路径。 | 1053| isInference | boolean | 否 | 是否从推理模型中导出权重。true表示从推理模型中导出权重,目前只支持true,默认为true。 | 1054| enableFp16 | boolean | 否 | 浮点权重是否以float16格式保存。true表示以float16格式保存,false表示不以float16格式保存。默认为false。 | 1055| changeableWeightsName | string[] | 否 | 设置可变权重的名称。默认为空字符串数组。 | 1056 1057**返回值:** 1058 1059| 类型 | 说明 | 1060| ------- | ------------------------------------------------------------ | 1061| boolean | 返回是否导出供micro推理使用的模型权重成功的结果。true表示导出供micro推理使用的模型权重成功,false表示导出供micro推理使用的模型权重失败。 | 1062 1063**示例:** 1064 1065```ts 1066let modelFile = '/path/to/xxx.ms'; 1067let microWeight = '/path/to/xxx.bin'; 1068mindSporeLite.loadTrainModelFromFile(modelFile).then((mindSporeLiteModel: mindSporeLite.Model) => { 1069 let ret = mindSporeLiteModel.exportWeightsCollaborateWithMicro(microWeight); 1070 if (ret == false) { 1071 console.error('MSLITE exportWeightsCollaborateWithMicro failed.') 1072 } 1073}) 1074``` 1075 1076## MSTensor 1077 1078模型张量实例。描述MSTensor对象的属性和方法。它与数组和矩阵非常相似,是MindSpore Lite网络运算中的基本数据结构。 1079 1080下例API示例中都需先使用[getInputs()](#getinputs)获取到MSTensor实例,再通过此实例调用对应方法。 1081 1082### 属性 1083 1084**系统能力:** SystemCapability.AI.MindSporeLite 1085 1086| 名称 | 类型 | 只读 | 可选 | 说明 | 1087| ---------- | --------------------- | ---- | ---- | ---------------------- | 1088| name | string | 否 | 否 | 张量的名称。 | 1089| shape | number[] | 否 | 否 | 张量的维度数组。 | 1090| elementNum | number | 否 | 否 | 张量的维度数组的长度。 | 1091| dataSize | number | 否 | 否 | 张量的数据的长度。 | 1092| dtype | [DataType](#datatype) | 否 | 否 | 张量的数据类型。 | 1093| format | [Format](#format) | 否 | 否 | 张量的数据排布方式。 | 1094 1095**示例:** 1096 1097```ts 1098let modelFile = '/path/to/xxx.ms'; 1099mindSporeLite.loadModelFromFile(modelFile).then((mindSporeLiteModel : mindSporeLite.Model) => { 1100 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 1101 console.info(modelInputs[0].name); 1102 console.info(modelInputs[0].shape.toString()); 1103 console.info(modelInputs[0].elementNum.toString()); 1104 console.info(modelInputs[0].dtype.toString()); 1105 console.info(modelInputs[0].format.toString()); 1106 console.info(modelInputs[0].dataSize.toString()); 1107}) 1108``` 1109 1110### getData 1111 1112getData(): ArrayBuffer 1113 1114获取张量的数据。 1115 1116**系统能力:** SystemCapability.AI.MindSporeLite 1117 1118**返回值:** 1119 1120| 类型 | 说明 | 1121| ----------- | -------------------- | 1122| ArrayBuffer | 返回张量的数据指针。 | 1123 1124**示例:** 1125 1126```ts 1127import { mindSporeLite } from '@kit.MindSporeLiteKit'; 1128import { common } from '@kit.AbilityKit'; 1129import { UIContext } from '@kit.ArkUI'; 1130 1131let inputName = 'input_data.bin'; 1132let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 1133globalContext.getApplicationContext().resourceManager.getRawFileContent(inputName).then(async (buffer : Uint8Array) => { 1134 let inputBuffer = buffer.buffer; 1135 let modelFile = '/path/to/xxx.ms'; 1136 let mindSporeLiteModel : mindSporeLite.Model = await mindSporeLite.loadModelFromFile(modelFile); 1137 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 1138 modelInputs[0].setData(inputBuffer); 1139 mindSporeLiteModel.predict(modelInputs).then((mindSporeLiteTensor : mindSporeLite.MSTensor[]) => { 1140 let output = new Float32Array(mindSporeLiteTensor[0].getData()); 1141 for (let i = 0; i < output.length; i++) { 1142 console.info(output[i].toString()); 1143 } 1144 }) 1145}) 1146``` 1147 1148### setData 1149 1150setData(inputArray: ArrayBuffer): void 1151 1152设置张量的数据。 1153 1154**系统能力:** SystemCapability.AI.MindSporeLite 1155 1156**参数:** 1157 1158| 参数名 | 类型 | 必填 | 说明 | 1159| ---------- | ----------- | ---- | ---------------------- | 1160| inputArray | ArrayBuffer | 是 | 张量的输入数据缓冲区。 | 1161 1162**示例:** 1163 1164```ts 1165import { mindSporeLite } from '@kit.MindSporeLiteKit'; 1166import { common } from '@kit.AbilityKit'; 1167import { UIContext } from '@kit.ArkUI'; 1168 1169let inputName = 'input_data.bin'; 1170let globalContext = new UIContext().getHostContext() as common.UIAbilityContext; 1171globalContext.getApplicationContext().resourceManager.getRawFileContent(inputName).then(async (buffer : Uint8Array) => { 1172 let inputBuffer = buffer.buffer; 1173 let modelFile = '/path/to/xxx.ms'; 1174 let mindSporeLiteModel : mindSporeLite.Model = await mindSporeLite.loadModelFromFile(modelFile); 1175 let modelInputs : mindSporeLite.MSTensor[] = mindSporeLiteModel.getInputs(); 1176 modelInputs[0].setData(inputBuffer); 1177}) 1178``` 1179 1180## DataType 1181 1182张量的数据类型。 1183 1184**系统能力:** SystemCapability.AI.MindSporeLite 1185 1186| 名称 | 值 | 说明 | 1187| ------------------- | ---- | ------------------- | 1188| TYPE_UNKNOWN | 0 | 未知类型。 | 1189| NUMBER_TYPE_INT8 | 32 | 保持Int8的类型。 | 1190| NUMBER_TYPE_INT16 | 33 | 保持Int16的类型。 | 1191| NUMBER_TYPE_INT32 | 34 | 保持Int32的类型。 | 1192| NUMBER_TYPE_INT64 | 35 | 保持Int64的类型。 | 1193| NUMBER_TYPE_UINT8 | 37 | 保持UInt8的类型。 | 1194| NUMBER_TYPE_UINT16 | 38 | 保持UInt16的类型。 | 1195| NUMBER_TYPE_UINT32 | 39 | 保持UInt32的类型。 | 1196| NUMBER_TYPE_UINT64 | 40 | 保持UInt64的类型。 | 1197| NUMBER_TYPE_FLOAT16 | 42 | 保持Float16的类型。 | 1198| NUMBER_TYPE_FLOAT32 | 43 | 保持Float32的类型。 | 1199| NUMBER_TYPE_FLOAT64 | 44 | 保持Float64的类型。 | 1200 1201## Format 1202 1203张量的数据排布方式。 1204 1205**系统能力:** SystemCapability.AI.MindSporeLite 1206 1207| 名称 | 值 | 说明 | 1208| -------------- | ---- | --------------------- | 1209| DEFAULT_FORMAT | -1 | 未知数据排布方式。 | 1210| NCHW | 0 | 数据排布方式为NCHW。 | 1211| NHWC | 1 | 数据排布方式为NHWC。 | 1212| NHWC4 | 2 | 数据排布方式为NHWC4。 | 1213| HWKC | 3 | 数据排布方式为HWKC。 | 1214| HWCK | 4 | 数据排布方式为HWCK。 | 1215| KCHW | 5 | 数据排布方式为KCHW。 | 1216