1 /* Copyright 2017 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; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import java.nio.ByteBuffer; 21 import java.util.HashMap; 22 import java.util.Map; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 import org.tensorflow.lite.flex.FlexDelegate; 27 import org.tensorflow.lite.nnapi.NnApiDelegate; 28 29 /** 30 * Unit tests for {@link org.tensorflow.lite.Interpreter} that validate execution with models that 31 * have TensorFlow ops. 32 */ 33 @RunWith(JUnit4.class) 34 public final class InterpreterFlexTest { 35 36 private static final ByteBuffer FLEX_MODEL_BUFFER = 37 TestUtils.getTestFileAsBuffer("tensorflow/lite/testdata/multi_add_flex.bin"); 38 39 /** Smoke test validating that flex model loading works when the flex delegate is used. */ 40 @Test testFlexModel()41 public void testFlexModel() throws Exception { 42 FlexDelegate delegate = new FlexDelegate(); 43 Interpreter.Options options = new Interpreter.Options().addDelegate(delegate); 44 try (Interpreter interpreter = new Interpreter(FLEX_MODEL_BUFFER, options)) { 45 testCommon(interpreter); 46 } finally { 47 delegate.close(); 48 } 49 } 50 51 /** Smoke test validating that flex model loading works when the flex delegate is linked. */ 52 @Test testFlexModelDelegateAutomaticallyApplied()53 public void testFlexModelDelegateAutomaticallyApplied() throws Exception { 54 try (Interpreter interpreter = new Interpreter(FLEX_MODEL_BUFFER)) { 55 testCommon(interpreter); 56 } 57 } 58 59 /** Smoke test validating that flex model loading works when the flex delegate is linked. */ 60 @Test testFlexModelDelegateAutomaticallyAppliedBeforeOtherDelegates()61 public void testFlexModelDelegateAutomaticallyAppliedBeforeOtherDelegates() throws Exception { 62 Interpreter.Options options = new Interpreter.Options(); 63 try (NnApiDelegate delegate = new NnApiDelegate(); 64 Interpreter interpreter = 65 new Interpreter(FLEX_MODEL_BUFFER, options.addDelegate(delegate))) { 66 testCommon(interpreter); 67 } 68 } 69 testCommon(Interpreter interpreter)70 private static void testCommon(Interpreter interpreter) { 71 assertThat(interpreter.getInputTensorCount()).isEqualTo(4); 72 assertThat(interpreter.getInputTensor(0).dataType()).isEqualTo(DataType.FLOAT32); 73 assertThat(interpreter.getInputTensor(1).dataType()).isEqualTo(DataType.FLOAT32); 74 assertThat(interpreter.getInputTensor(2).dataType()).isEqualTo(DataType.FLOAT32); 75 assertThat(interpreter.getInputTensor(3).dataType()).isEqualTo(DataType.FLOAT32); 76 assertThat(interpreter.getOutputTensorCount()).isEqualTo(2); 77 assertThat(interpreter.getOutputTensor(0).dataType()).isEqualTo(DataType.FLOAT32); 78 assertThat(interpreter.getOutputTensor(1).dataType()).isEqualTo(DataType.FLOAT32); 79 80 float[] input1 = {1}; 81 float[] input2 = {2}; 82 float[] input3 = {3}; 83 float[] input4 = {5}; 84 Object[] inputs = new Object[] {input1, input2, input3, input4}; 85 86 float[] parsedOutput1 = new float[1]; 87 float[] parsedOutput2 = new float[1]; 88 Map<Integer, Object> outputs = new HashMap<>(); 89 outputs.put(0, parsedOutput1); 90 outputs.put(1, parsedOutput2); 91 92 interpreter.runForMultipleInputsOutputs(inputs, outputs); 93 94 float[] expectedOutput1 = {6}; 95 float[] expectedOutput2 = {10}; 96 assertThat(parsedOutput1).usingTolerance(0.1f).containsExactly(expectedOutput1).inOrder(); 97 assertThat(parsedOutput2).usingTolerance(0.1f).containsExactly(expectedOutput2).inOrder(); 98 } 99 100 static { FlexDelegate.initTensorFlowForTesting()101 FlexDelegate.initTensorFlowForTesting(); 102 } 103 } 104