xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/java/src/testhelper/java/org/tensorflow/lite/TestHelper.java (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 /** A helper class for internal tests. */
19 public class TestHelper {
20 
21   /**
22    * Gets the last inference duration in nanoseconds. It returns null if there is no previous
23    * inference run or the last inference run failed.
24    *
25    * @param interpreter an instance of {@code Interpreter}. If it is not initialized, an {@code
26    *     IllegalArgumentException} will be thrown.
27    */
getLastNativeInferenceDurationNanoseconds(Interpreter interpreter)28   public static Long getLastNativeInferenceDurationNanoseconds(Interpreter interpreter) {
29     if (interpreter != null && interpreter.wrapper != null) {
30       return interpreter.wrapper.getLastNativeInferenceDurationNanoseconds();
31     } else {
32       throw new IllegalArgumentException("Interpreter has not initialized; Failed to get latency.");
33     }
34   }
35 
36   /**
37    * Gets the dimensions of an input.
38    *
39    * @param interpreter an instance of {@code Interpreter}. If it is not initialized, an {@code
40    *     IllegalArgumentException} will be thrown.
41    * @param index an integer index of the input. If it is invalid, an {@code
42    *     IllegalArgumentException} will be thrown.
43    */
getInputDims(Interpreter interpreter, int index)44   public static int[] getInputDims(Interpreter interpreter, int index) {
45     if (interpreter != null && interpreter.wrapper != null) {
46       return interpreter.wrapper.getInputTensor(index).shape();
47     } else {
48       throw new IllegalArgumentException(
49           "Interpreter has not initialized;" + " Failed to get input dimensions.");
50     }
51   }
52 
53   /**
54    * Gets the string name of the data type of an input.
55    *
56    * @param interpreter an instance of {@code Interpreter}. If it is not initialized, an {@code
57    *     IllegalArgumentException} will be thrown.
58    * @param index an integer index of the input. If it is invalid, an {@code
59    *     IllegalArgumentException} will be thrown.
60    * @return string name of the data type. Possible values include "float", "int", "byte", and
61    *     "long".
62    */
getInputDataType(Interpreter interpreter, int index)63   public static String getInputDataType(Interpreter interpreter, int index) {
64     if (interpreter != null && interpreter.wrapper != null) {
65       return DataTypeUtils.toStringName(interpreter.wrapper.getInputTensor(index).dataType());
66     } else {
67       throw new IllegalArgumentException(
68           "Interpreter has not initialized;" + " Failed to get input data type.");
69     }
70   }
71 
72   /**
73    * Gets the string name of the data type of an output.
74    *
75    * @param interpreter an instance of {@code Interpreter}. If it is not initialized, an {@code
76    *     IllegalArgumentException} will be thrown.
77    * @param index an integer index of the output. If it is invalid, an {@code
78    *     IllegalArgumentException} will be thrown.
79    * @return string name of the data type. Possible values include "float", "int", "byte", and
80    *     "long".
81    */
getOutputDataType(Interpreter interpreter, int index)82   public static String getOutputDataType(Interpreter interpreter, int index) {
83     if (interpreter != null && interpreter.wrapper != null) {
84       return DataTypeUtils.toStringName(interpreter.wrapper.getOutputTensor(index).dataType());
85     } else {
86       throw new IllegalArgumentException(
87           "Interpreter has not initialized;" + " Failed to get output data type.");
88     }
89   }
90 }
91