From d77d8b3f4ac68e82664fa8d183f035d903b75295 Mon Sep 17 00:00:00 2001 From: Chao Mei Date: Tue, 3 Nov 2020 22:19:10 -0800 Subject: [PATCH] Deprecate the modifyGraphWithDelegate method in Java binding. The Options.addDelegate(...) is preferred to provide delegates at creation time. PiperOrigin-RevId: 340586749 Change-Id: Ib7af63469165253206e906379c47a56f46445190 --- .../java/org/tensorflow/lite/Interpreter.java | 17 +++++++---------- .../org/tensorflow/lite/InterpreterTest.java | 11 +++++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tensorflow/lite/java/src/main/java/org/tensorflow/lite/Interpreter.java b/tensorflow/lite/java/src/main/java/org/tensorflow/lite/Interpreter.java index e2044212340..cea4a4ae2c2 100644 --- a/tensorflow/lite/java/src/main/java/org/tensorflow/lite/Interpreter.java +++ b/tensorflow/lite/java/src/main/java/org/tensorflow/lite/Interpreter.java @@ -369,6 +369,7 @@ public final class Interpreter implements AutoCloseable { *

Note: This call is *purely optional*. Tensor allocation will occur automatically during * execution if any input tensors have been resized. This call is most useful in determining the * shapes for any output tensors before executing the graph, e.g., + * *

{@code
    * interpreter.resizeInput(0, new int[]{1, 4, 4, 3}));
    * interpreter.allocateTensors();
@@ -500,14 +501,11 @@ public final class Interpreter implements AutoCloseable {
   /**
    * Advanced: Modifies the graph with the provided {@link Delegate}.
    *
-   * 

Note: The typical path for providing delegates is via {@link Options#addDelegate}, at - * creation time. This path should only be used when a delegate might require coordinated - * interaction between Interpeter creation and delegate application. - * - *

WARNING: This is an experimental API and subject to change. - * * @throws IllegalArgumentException if error occurs when modifying graph with {@code delegate}. + * @deprecated Prefer using {@link Options#addDelegate} to provide delegates at creation time. + * This method will be removed in a future release. */ + @Deprecated public void modifyGraphWithDelegate(Delegate delegate) { checkNotClosed(); wrapper.modifyGraphWithDelegate(delegate); @@ -525,7 +523,7 @@ public final class Interpreter implements AutoCloseable { wrapper.resetVariableTensors(); } - /** + /** * Advanced: Interrupts inference in the middle of a call to {@link Interpreter#run}. * *

A cancellation flag will be set to true when this function gets called. The interpreter will @@ -536,10 +534,9 @@ public final class Interpreter implements AutoCloseable { *

WARNING: This is an experimental API and subject to change. * * @param cancelled {@code true} to cancel inference in a best-effort way; {@code false} to - * resume. + * resume. * @throws IllegalStateException if the interpreter is not initialized with the cancellable - * option, which is by default off. - * + * option, which is by default off. * @see {@link Interpreter.Options#setCancellable(boolean)}. */ public void setCancelled(boolean cancelled) { diff --git a/tensorflow/lite/java/src/test/java/org/tensorflow/lite/InterpreterTest.java b/tensorflow/lite/java/src/test/java/org/tensorflow/lite/InterpreterTest.java index f7e10ae796c..7009f069bd8 100644 --- a/tensorflow/lite/java/src/test/java/org/tensorflow/lite/InterpreterTest.java +++ b/tensorflow/lite/java/src/test/java/org/tensorflow/lite/InterpreterTest.java @@ -534,6 +534,8 @@ public final class InterpreterTest { } @Test + // modifyGraphWithDelegate(...) is deprecated, suppress the warning to allow testing. + @SuppressWarnings("deprecation") public void testModifyGraphWithDelegate() throws Exception { System.loadLibrary("tensorflowlite_test_jni"); Delegate delegate = @@ -543,7 +545,8 @@ public final class InterpreterTest { return getNativeHandleForDelegate(); } }; - Interpreter interpreter = new Interpreter(MODEL_BUFFER); + Interpreter interpreter = + new Interpreter(MODEL_BUFFER, new Interpreter.Options().setUseXNNPACK(false)); interpreter.modifyGraphWithDelegate(delegate); // The native delegate stubs out the graph with a single op that produces the scalar value 7. @@ -640,8 +643,8 @@ public final class InterpreterTest { public void testCancelInference() throws Exception { float[][][][] inputs = new float[2][8][8][3]; float[][][][] parsedOutputs = new float[2][8][8][3]; - Interpreter interpreter = new Interpreter( - MODEL_BUFFER, new Interpreter.Options().setCancellable(true)); + Interpreter interpreter = + new Interpreter(MODEL_BUFFER, new Interpreter.Options().setCancellable(true)); // Part 1: Should be interrupted when flag is set to true. try { @@ -649,7 +652,7 @@ public final class InterpreterTest { interpreter.run(inputs, parsedOutputs); fail(); } catch (IllegalArgumentException e) { - // TODO(b/168266570): Return InterruptedException. + // TODO(b/168266570): Return InterruptedException. assertThat(e) .hasMessageThat() .contains(