2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.samsung.android.beyond.inference;
20 import android.util.Log;
22 import com.samsung.android.beyond.inference.tensor.TensorSet;
23 import com.samsung.android.beyond.NativeInstance;
25 import static com.samsung.android.beyond.inference.Option.TAG;
27 import androidx.annotation.NonNull;
29 public class InferenceHandler extends NativeInstance {
30 private List<Peer> peerList = new ArrayList<Peer>();
32 private TensorOutputCallback tensorOutputCallback;
34 InferenceHandler(@NonNull InferenceMode inferenceMode) {
35 long nativeInstance = create(inferenceMode.toString());
36 if (nativeInstance == 0L) {
37 throw new RuntimeException("The native instance of InferenceHandler is not created successfully.");
40 registerNativeInstance(nativeInstance, (Long instance) -> destroy(instance));
44 // removePeer() should be provided
45 public boolean addInferencePeer(@NonNull Peer inferencePeer) {
47 Log.e(TAG, "Instance is invalid.");
51 if (inferencePeer.getNativeInstance() == 0L) {
52 Log.e(TAG, "The given peer instance is invalid.");
56 if (addPeer(instance, inferencePeer.getNativeInstance()) == true) {
58 // Hold the peer instance until release them from the this.
59 peerList.add(inferencePeer);
66 public boolean loadModel(@NonNull String modelPath) {
68 Log.e(TAG, "Instance is invalid.");
72 Log.i(TAG, "Model path in a client : " + modelPath);
73 return loadModel(instance, modelPath);
76 public boolean prepare() {
78 Log.e(TAG, "Instance is invalid.");
82 return prepare(instance);
85 public boolean run(@NonNull TensorSet inputTensors) {
87 Log.e(TAG, "Instance is invalid.");
91 if (inputTensors.getTensorsInstance() == 0L) {
92 Log.e(TAG, "The given tensor set is invalid.");
96 return run(instance, inputTensors.getTensorsInstance(), inputTensors.getTensors().length);
99 public boolean setOutputCallback(@NonNull TensorOutputCallback tensorOutputCallback) {
100 if (instance == 0L) {
101 Log.e(TAG, "Instance is invalid.");
105 this.tensorOutputCallback = tensorOutputCallback;
107 if (setCallback(instance, tensorOutputCallback) < 0) {
108 Log.e(TAG, "Fail to set the given output callback.");
115 private native long create(String inferenceMode);
117 private native boolean addPeer(long inferenceHandle, long peerHandle);
119 private native boolean loadModel(long handle, String modelPath);
121 private native boolean prepare(long inferenceHandle);
123 private native boolean run(long inferenceHandle, long tensorsInstance, int numberOfTensors);
125 private static native void destroy(long inferenceHandle);
127 private native int setCallback(long instance, TensorOutputCallback tensorOutputCallback);