public final class InferenceSession
- Object
- InferenceSession
ImplementsAutoCloseable
Reusable, native on-device session for a TensorFlow Lite model.
Opening and execution are asynchronous because model allocation and
delegates can be expensive. Metadata and resize operations are synchronous.
A session is not usable after close(); applications should retain
and reuse one session instead of reopening the model for every input.
Methods
public static boolean isSupported() | Tests whether the current port includes a native LiteRT runtime. |
public static AsyncResource<InferenceSession> open(ModelSource source, InferenceOptions options) | Opens and allocates a model session off the EDT. |
public TensorInfo[] getInputs() | Returns the model’s current input metadata. |
public TensorInfo[] getOutputs() | Returns the model’s current output metadata. |
public AsyncResource<Tensor[]> run(Tensor[] inputs) | Copies input tensors to native memory, invokes the model, and returns every output tensor. |
public void resizeInput(String name, int[] shape) | Resizes an input and reallocates native tensors before the next run. |
public void close() | Releases the interpreter, delegates, and any temporary staged model. |
Inherited methods
Method details
isSupported
public static boolean isSupported()Returns
true when sessions can be opened on this targetopen
public static AsyncResource<InferenceSession> open(ModelSource source, InferenceOptions options)Opens and allocates a model session off the EDT.
The option values are copied before asynchronous backend work is
scheduled. Reusing or changing the supplied InferenceOptions
after this method returns therefore cannot alter the pending open.
Canceling the returned resource prevents session publication; if the
native backend finishes opening afterward, its handle is closed
automatically.
Parameters
sourceModelSource- bytes, resource, or file containing a
.tflitemodel optionsInferenceOptions- execution options;
nulluses defaults
Returns
InferenceException
when the model or requested accelerator cannot be usedgetInputs
public TensorInfo[] getInputs()resizeInput(String, int[]) call. Metadata
cannot be queried while run(Tensor[]) is pending because the
native interpreter is mutable and may be updating tensor state.Returns
Throws
IllegalStateException- if the session is closed or a run is pending
getOutputs
public TensorInfo[] getOutputs()run(Tensor[]) is pending
because the native interpreter is mutable and may be updating tensor
state.Returns
Throws
IllegalStateException- if the session is closed or a run is pending
run
public AsyncResource<Tensor[]> run(Tensor[] inputs)close() while this operation
is pending prevents new work immediately but defers native release until
the returned resource succeeds or fails. A session accepts one run at a
time because the underlying native interpreter is mutable. The array
container is defensively copied before it is handed to the asynchronous
backend, so replacing an element after this method returns cannot change
the pending invocation. Each Tensor is itself immutable.
Canceling the returned resource suppresses result publication but does
not interrupt an invocation that has already entered LiteRT. The
session remains busy, and a pending close() remains deferred,
until the native operation actually succeeds or fails.Parameters
inputsTensor[]- one tensor for each model input;
nullis treated as an empty input array
Returns
Throws
IllegalStateException- if the session is closed or already running
IllegalArgumentException- if an input name, count, or shape does not match the model’s current input metadata
resizeInput
public void resizeInput(String name, int[] shape)Resizes an input and reallocates native tensors before the next run.
This method throws while an asynchronous run(Tensor[]) is
pending because native runtimes cannot safely reallocate tensors during
an invocation.
Parameters
nameString- model input name, or
nullfor the first input shapeint[]- new non-negative dimensions
Throws
IllegalStateException- if the session is closed or a run is pending
close
public void close()ModelSource.file(String) is never
deleted. If a run is pending, release is deferred until that run
settles. Calling this method more than once has no effect.