SignatureRunner class

A runner for a specific model signature, enabling named tensor access.

Signatures allow models to expose multiple named entry points. This is the foundation for on-device training, where a model typically exposes:

  • train: runs one training step (forward pass + backprop + optimizer)
  • infer: runs inference / prediction
  • get_weights: reads current model weights into Dart buffers
  • set_weights: writes weight values from Dart buffers into the model

On-device training example

// Load a model that was exported with training signatures.
final interpreter = await Interpreter.fromAsset('trainable_model.tflite');

// Optionally inspect available signatures:
print(interpreter.signatureKeys); // ['train', 'infer', 'get_weights', 'set_weights']

// --- Training loop ---
final trainRunner = interpreter.getSignatureRunner('train');

for (int epoch = 0; epoch < 10; epoch++) {
  final lossBuffer = Float32List(1);
  trainRunner.run(
    {'x': imageData, 'y': labels},
    {'loss': lossBuffer},
  );
  print('Epoch $epoch loss: ${lossBuffer[0]}');
}
trainRunner.close();

// --- Inference ---
final inferRunner = interpreter.getSignatureRunner('infer');
final predictions = Float32List(10);
inferRunner.run({'x': testImage}, {'output': predictions});
inferRunner.close();

// --- Weight persistence (get/set) ---
final getRunner = interpreter.getSignatureRunner('get_weights');
final w = [[0.0]];
final b = [0.0];
getRunner.run({}, {'w': w, 'b': b});
getRunner.close();

// Restore weights into a fresh interpreter:
final setRunner = freshInterpreter.getSignatureRunner('set_weights');
setRunner.run({'w': w, 'b': b}, {});
setRunner.close();

interpreter.close();

Building a training-capable model (Python)

Export a LiteRT model with train, infer, get_weights, and set_weights signatures and convert with resource variables enabled:

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.experimental_enable_resource_variables = True
tflite_model = converter.convert()

Constructors

SignatureRunner(Pointer<TfLiteSignatureRunner> _runner)
Creates a SignatureRunner from a raw native pointer.

Properties

hashCode → int
The hash code for this object.
no setterinherited
inputCount → int
The number of input tensors for this signature.
no setter
inputNames → List<String>
The names of all input tensors for this signature.
no setter
isClosed → bool
Whether close has been called on this runner.
no setter
lastInferenceDurationMicroseconds → int
Duration of the most recent invocation in microseconds.
no setter
lastNativeInferenceDurationMicroSeconds → int
Duration of the most recent native invocation in microseconds.
no setter
outputCount → int
The number of output tensors for this signature.
no setter
outputNames → List<String>
The names of all output tensors for this signature.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

allocateTensors() → void
Allocates memory for all tensors in this signature.
cancel() → bool
Attempts to cancel the currently executing invoke call.
close() → void
Destroys this signature runner and releases native resources.
getInputName(int index) → String
Returns the name of the input tensor at index.
getInputTensor(String name) → Tensor
Returns the input Tensor identified by name.
getInputTensors() → List<Tensor>
Returns all input tensors for this signature.
getOutputName(int index) → String
Returns the name of the output tensor at index.
getOutputTensor(String name) → Tensor
Returns the output Tensor identified by name.
getOutputTensors() → List<Tensor>
Returns all output tensors for this signature.
invoke() → void
Runs this signature.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
resizeInputTensor(String name, List<int> shape) → void
Resizes the input tensor identified by name to shape.
run(Map<String, Object> inputs, Map<String, Object> outputs) → void
Runs this signature with named inputs, writing results to outputs.
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited