Bolt
BoltModel.java
Go to the documentation of this file.
1 
22 package com.huawei.noah;
23 
24 import java.io.FileNotFoundException;
25 
26 public final class BoltModel implements Cloneable {
27  static
28  {
29  loadLibrary("c++_shared", true);
30  //loadLibrary("OpenCL", false);
31  loadLibrary("GLES_mali", false);
32  loadLibrary("BoltModel", false);
33  }
34 
35  public BoltModel()
36  {
37  this.modelAddr = 0;
38  this.IResult = -1;
39  }
40 
63  public BoltModel(String modelPath,
64  AffinityType affinity,
65  int inputNum,
66  String[] inputName,
67  int[] inputN,
68  int[] inputC,
69  int[] inputH,
70  int[] inputW,
71  DataType[] inputDataType,
72  DataFormat[] inputDataFormat)
73  {
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();
79  }
80 
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);
86  } else {
87  this.IResult = -1;
88  System.err.println("[ERROR] model cannot be created in " + this.getClass().getName());
89  }
90  }
91 
116  public BoltModel(String modelPath,
117  AffinityType affinity,
118  int inputNum,
119  String[] inputName,
120  int[] inputN,
121  int[] inputC,
122  int[] inputH,
123  int[] inputW,
124  DataType[] inputDataType,
125  DataFormat[] inputDataFormat,
126  int outputNum,
127  String[] outputName)
128  {
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();
134  }
135 
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);
141  } else {
142  this.IResult = -1;
143  System.err.println("[ERROR] model cannot be created in " + this.getClass().getName());
144  }
145  }
146 
152  protected Object clone()
153  {
154  BoltModel cloneModel = new BoltModel();
155  if (0 != this.modelAddr) {
156  cloneModel.modelAddr = cloneModel(this.modelAddr);
157  } else {
158  cloneModel.modelAddr = 0;
159  }
160  if (-1 != this.IResult) {
161  cloneModel.IResult = cloneResultHandle(this.IResult);
162  } else {
163  cloneModel.IResult = -1;
164  }
165  return cloneModel;
166  }
167 
175  public void setRuntimeDevice(int cpuId, DeviceType device) throws FileNotFoundException
176  {
177  if (0 == this.modelAddr) {
178  throw new FileNotFoundException();
179  }
180  setRuntimeDeviceJNI(this.modelAddr, cpuId, device.toString());
181  }
182 
188  public void setRuntimeDeviceDynamic() throws FileNotFoundException
189  {
190  if (0 == this.modelAddr) {
191  throw new FileNotFoundException();
192  }
193  setRuntimeDeviceDynamicJNI(this.modelAddr);
194  }
195 
205  public native void setNumThreads(int threads);
206 
215  public BoltResult run(int inputNum, String[] inputName, float[][] inputData)
216  {
217  if (0 == this.modelAddr) {
218  return null;
219  }
220  runModel(this.modelAddr, this.IResult, inputNum, inputName, inputData);
221  BoltResult boltResult =
222  getOutput(this.IResult, BoltResult.class.getName().replace(".", "/"));
223  return boltResult;
224  }
225 
240  public BoltResult run(int inputNum,
241  String[] inputName,
242  int[] inputN,
243  int[] inputC,
244  int[] inputH,
245  int[] inputW,
246  DataType[] inputDataType,
247  DataFormat[] inputDataFormat,
248  float[][] inputData)
249  {
250  if (0 == this.modelAddr) {
251  return null;
252  }
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();
258  }
259 
260  resizeModelInput(this.modelAddr, inputNum, inputName, inputN, inputC, inputH, inputW,
261  inputDataTypeString, inputDataFormatString);
262  runModel(this.modelAddr, this.IResult, inputNum, inputName, inputData);
263  BoltResult boltResult =
264  getOutput(this.IResult, BoltResult.class.getName().replace(".", "/"));
265  return boltResult;
266  }
267 
273  public void destructor()
274  {
275  if (-1 != this.IResult) {
276  freeResultHandle(this.IResult);
277  this.IResult = -1;
278  }
279  if (0 != this.modelAddr) {
280  destroyModel(this.modelAddr);
281  this.modelAddr = 0;
282  }
283  }
284 
285  private long modelAddr;
286 
287  private long IResult;
288 
289  private native long createModel(String modelPath, String affinity);
290 
291  private native void prepareModel(long modelAddr,
292  int inputNum,
293  String[] inputName,
294  int[] inputN,
295  int[] inputC,
296  int[] inputH,
297  int[] inputW,
298  String[] inputDataType,
299  String[] inputDataFormat);
300 
301  private native long cloneModel(long modelAddr);
302 
303  private native long cloneResultHandle(long IResult);
304 
305  private native void resizeModelInput(long modelAddr,
306  int inputNum,
307  String[] inputName,
308  int[] inputN,
309  int[] inputC,
310  int[] inputH,
311  int[] inputW,
312  String[] inputDataType,
313  String[] inputDataFormat);
314 
315  private native long allocAllResultHandle(long modelAddr);
316 
317  private native long allocSpecificResultHandle(
318  long modelAddr, int outputNum, String[] outputName);
319 
320  private native void setRuntimeDeviceJNI(long modelAddr, int cpuId, String device);
321 
322  private native void setRuntimeDeviceDynamicJNI(long modelAddr);
323 
324  private native void runModel(
325  long modelAddr, long IResult, int inputNum, String[] inputName, float[][] inputData);
326 
327  private native BoltResult getOutput(long IResult, String boltResultPath);
328 
329  private native void freeResultHandle(long IResult);
330 
331  private native void destroyModel(long modelAddr);
332 
333  private static void loadLibrary(String libraryName, boolean optional)
334  {
335  try {
336  System.loadLibrary(libraryName);
337  } catch (UnsatisfiedLinkError e) {
338  if (!optional) {
339  e.printStackTrace();
340  }
341  }
342  }
343 }
void setRuntimeDevice(int cpuId, DeviceType device)
set process to run on specified CPU core
Definition: BoltModel.java:175
BoltResult run(int inputNum, String[] inputName, float[][] inputData)
inference result from input
Definition: BoltModel.java:215
void destructor()
recycle memory and destroy model
Definition: BoltModel.java:273
void setRuntimeDeviceDynamic()
set process cpu affinity according cpu average occupy
Definition: BoltModel.java:188
Object clone()
clone BoltModel
Definition: BoltModel.java:152
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
Definition: BoltModel.java:116
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
Definition: BoltModel.java:240
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
Definition: BoltModel.java:63