1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 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 16 package org.tensorflow.lite.gpu; 17 18 import java.io.Closeable; 19 20 /** 21 * GPU Delegate CompatibilityListing data. 22 * 23 * <p>The GPU delegate is not supported on all Android devices, due to differences in available 24 * OpenGL versions, driver features, and device resources. This class provides information on 25 * whether the GPU delegate is suitable for the current device. 26 * 27 * <p>This API is experimental and subject to change. 28 * 29 * <p><b>WARNING:</b> the compatibilityList is constructed from testing done on a limited set of 30 * models. You should plan to verify that your own model(s) work. 31 * 32 * <p>Example usage: 33 * 34 * <pre>{@code 35 * Interpreter.Options options = new Interpreter.Options(); 36 * try (CompatibilityList compatibilityList = new CompatibilityList()) { 37 * if (compatibilityList.isDelegateSupportedOnThisDevice()) { 38 * GpuDelegate.Options delegateOptions = compatibilityList.getBestOptionsForThisDevice(); 39 * gpuDelegate = new GpuDelegate(delegateOptions): 40 * options.addDelegate(gpuDelegate); 41 * } 42 * } 43 * Interpreter interpreter = new Interpreter(modelBuffer, options); 44 * }</pre> 45 */ 46 public class CompatibilityList implements Closeable { 47 48 private static final long INVALID_COMPATIBILITY_LIST_HANDLE = 0; 49 private static final String TFLITE_GPU_LIB = "tensorflowlite_gpu_jni"; 50 51 private long compatibilityListHandle = INVALID_COMPATIBILITY_LIST_HANDLE; 52 53 /** Whether the GPU delegate is supported on this device. */ isDelegateSupportedOnThisDevice()54 public boolean isDelegateSupportedOnThisDevice() { 55 if (compatibilityListHandle == INVALID_COMPATIBILITY_LIST_HANDLE) { 56 throw new IllegalStateException("Trying to query a closed compatibilityList."); 57 } 58 return nativeIsDelegateSupportedOnThisDevice(compatibilityListHandle); 59 } 60 61 /** What options should be used for the GPU delegate. */ getBestOptionsForThisDevice()62 public GpuDelegate.Options getBestOptionsForThisDevice() { 63 // For forward compatibility, when the compatibilityList contains more information. 64 return new GpuDelegate.Options(); 65 } 66 CompatibilityList()67 public CompatibilityList() { 68 compatibilityListHandle = createCompatibilityList(); 69 } 70 71 /** 72 * Frees TFLite resources in C runtime. 73 * 74 * <p>User is expected to call this method explicitly. 75 */ 76 @Override close()77 public void close() { 78 if (compatibilityListHandle != INVALID_COMPATIBILITY_LIST_HANDLE) { 79 deleteCompatibilityList(compatibilityListHandle); 80 compatibilityListHandle = INVALID_COMPATIBILITY_LIST_HANDLE; 81 } 82 } 83 84 static { 85 System.loadLibrary(TFLITE_GPU_LIB); 86 } 87 createCompatibilityList()88 private static native long createCompatibilityList(); 89 deleteCompatibilityList(long handle)90 private static native void deleteCompatibilityList(long handle); 91 nativeIsDelegateSupportedOnThisDevice(long handle)92 private static native boolean nativeIsDelegateSupportedOnThisDevice(long handle); 93 } 94