22 package com.huawei.noah;
24 import java.io.FileNotFoundException;
29 loadLibrary(
"c++_shared",
true);
31 loadLibrary(
"GLES_mali",
false);
32 loadLibrary(
"BoltModel",
false);
74 String[] inputDataTypeString =
new String[inputNum];
75 String[] inputDataFormatString =
new String[inputNum];
76 for (
int i = 0; i < inputNum; i++) {
77 inputDataTypeString[i] = inputDataType[i].toString();
78 inputDataFormatString[i] = inputDataFormat[i].toString();
81 this.modelAddr = createModel(modelPath, affinity.toString());
82 if (0 != this.modelAddr) {
83 prepareModel(this.modelAddr, inputNum, inputName, inputN, inputC, inputH, inputW,
84 inputDataTypeString, inputDataFormatString);
85 this.IResult = allocAllResultHandle(this.modelAddr);
88 System.err.println(
"[ERROR] model cannot be created in " + this.getClass().getName());
129 String[] inputDataTypeString =
new String[inputNum];
130 String[] inputDataFormatString =
new String[inputNum];
131 for (
int i = 0; i < inputNum; i++) {
132 inputDataTypeString[i] = inputDataType[i].toString();
133 inputDataFormatString[i] = inputDataFormat[i].toString();
136 this.modelAddr = createModel(modelPath, affinity.toString());
137 if (0 != this.modelAddr) {
138 prepareModel(this.modelAddr, inputNum, inputName, inputN, inputC, inputH, inputW,
139 inputDataTypeString, inputDataFormatString);
140 this.IResult = allocSpecificResultHandle(this.modelAddr, outputNum, outputName);
143 System.err.println(
"[ERROR] model cannot be created in " + this.getClass().getName());
155 if (0 != this.modelAddr) {
156 cloneModel.modelAddr = cloneModel(this.modelAddr);
158 cloneModel.modelAddr = 0;
160 if (-1 != this.IResult) {
161 cloneModel.IResult = cloneResultHandle(this.IResult);
163 cloneModel.IResult = -1;
177 if (0 == this.modelAddr) {
178 throw new FileNotFoundException();
180 setRuntimeDeviceJNI(this.modelAddr, cpuId, device.toString());
190 if (0 == this.modelAddr) {
191 throw new FileNotFoundException();
193 setRuntimeDeviceDynamicJNI(this.modelAddr);
215 public BoltResult run(
int inputNum, String[] inputName,
float[][] inputData)
217 if (0 == this.modelAddr) {
220 runModel(this.modelAddr, this.IResult, inputNum, inputName, inputData);
222 getOutput(this.IResult,
BoltResult.class.getName().replace(
".",
"/"));
250 if (0 == this.modelAddr) {
253 String[] inputDataTypeString =
new String[inputNum];
254 String[] inputDataFormatString =
new String[inputNum];
255 for (
int i = 0; i < inputNum; i++) {
256 inputDataTypeString[i] = inputDataType[i].toString();
257 inputDataFormatString[i] = inputDataFormat[i].toString();
260 resizeModelInput(this.modelAddr, inputNum, inputName, inputN, inputC, inputH, inputW,
261 inputDataTypeString, inputDataFormatString);
262 runModel(this.modelAddr, this.IResult, inputNum, inputName, inputData);
264 getOutput(this.IResult,
BoltResult.class.getName().replace(
".",
"/"));
275 if (-1 != this.IResult) {
276 freeResultHandle(this.IResult);
279 if (0 != this.modelAddr) {
280 destroyModel(this.modelAddr);
285 private long modelAddr;
287 private long IResult;
289 private native
long createModel(String modelPath, String affinity);
291 private native
void prepareModel(
long modelAddr,
298 String[] inputDataType,
299 String[] inputDataFormat);
301 private native
long cloneModel(
long modelAddr);
303 private native
long cloneResultHandle(
long IResult);
305 private native
void resizeModelInput(
long modelAddr,
312 String[] inputDataType,
313 String[] inputDataFormat);
315 private native
long allocAllResultHandle(
long modelAddr);
317 private native
long allocSpecificResultHandle(
318 long modelAddr,
int outputNum, String[] outputName);
320 private native
void setRuntimeDeviceJNI(
long modelAddr,
int cpuId, String device);
322 private native
void setRuntimeDeviceDynamicJNI(
long modelAddr);
324 private native
void runModel(
325 long modelAddr,
long IResult,
int inputNum, String[] inputName,
float[][] inputData);
327 private native
BoltResult getOutput(
long IResult, String boltResultPath);
329 private native
void freeResultHandle(
long IResult);
331 private native
void destroyModel(
long modelAddr);
333 private static void loadLibrary(String libraryName,
boolean optional)
336 System.loadLibrary(libraryName);
337 }
catch (UnsatisfiedLinkError e) {
void setRuntimeDevice(int cpuId, DeviceType device)
set process to run on specified CPU core
BoltResult run(int inputNum, String[] inputName, float[][] inputData)
inference result from input
void destructor()
recycle memory and destroy model
void setRuntimeDeviceDynamic()
set process cpu affinity according cpu average occupy
Object clone()
clone BoltModel
BoltModel(String modelPath, AffinityType affinity, int inputNum, String[] inputName, int[] inputN, int[] inputC, int[] inputH, int[] inputW, DataType[] inputDataType, DataFormat[] inputDataFormat, int outputNum, String[] outputName)
initial model and alloc memory, and the output is decided by user
BoltResult run(int inputNum, String[] inputName, int[] inputN, int[] inputC, int[] inputH, int[] inputW, DataType[] inputDataType, DataFormat[] inputDataFormat, float[][] inputData)
inference result from resized input
native void setNumThreads(int threads)
set parallel threads num
BoltModel(String modelPath, AffinityType affinity, int inputNum, String[] inputName, int[] inputN, int[] inputC, int[] inputH, int[] inputW, DataType[] inputDataType, DataFormat[] inputDataFormat)
initial model and alloc memory