runInference method
Run inference on a session
sessionId
is the ID of the session to run inference on
inputs
is a map of input names to OrtValue objects
runOptions
is an optional map of run options
Implementation
@override
Future<Map<String, dynamic>> runInference(
String sessionId,
Map<String, OrtValue> inputs, {
Map<String, dynamic>? runOptions,
}) async {
try {
// Check if the session exists
if (!_sessions.containsKey(sessionId)) {
throw PlatformException(code: "INVALID_SESSION", message: "Session not found", details: null);
}
final session = _sessions[sessionId]!;
// Create a JavaScript object for inputs
final jsInputs = createJSObject();
// Process inputs - expecting OrtValue references
for (final entry in inputs.entries) {
final name = entry.key;
final ortValue = entry.value;
// Get the tensor by ID
final valueId = ortValue.id;
final tensor = _ortValues[valueId];
if (tensor == null) {
throw PlatformException(code: "INVALID_VALUE", message: "OrtValue with ID $valueId not found", details: null);
}
// Add to inputs object
jsInputs.setProperty(name.toJS, tensor);
}
// Create run options if provided
JSObject? jsRunOptions;
if (runOptions != null && runOptions.isNotEmpty) {
jsRunOptions = createJSObject();
// Set run options if needed
if (runOptions.containsKey('logSeverityLevel')) {
jsRunOptions.setProperty('logSeverityLevel'.toJS, runOptions['logSeverityLevel']);
}
if (runOptions.containsKey('logVerbosityLevel')) {
jsRunOptions.setProperty('logVerbosityLevel'.toJS, runOptions['logVerbosityLevel']);
}
if (runOptions.containsKey('terminate')) {
jsRunOptions.setProperty('terminate'.toJS, runOptions['terminate']);
}
}
// Run inference
final runPromise =
jsRunOptions != null
? callMethod(session, 'run', [jsInputs, jsRunOptions])
: callMethod(session, 'run', [jsInputs]);
// Wait for the promise to resolve
final jsOutputs = await promiseToFuture<JSObject>(runPromise);
// Process outputs - create OrtValue objects for each output
final outputMap = <String, dynamic>{};
final outputNames = getOutputNames(session);
for (final name in outputNames) {
if (jsOutputs.has(name)) {
final tensor = jsOutputs.getProperty(name.toJS) as JSObject;
// Create a new OrtValue and store it
final valueId = '${DateTime.now().millisecondsSinceEpoch}_${math.Random().nextInt(10000)}';
_ortValues[valueId] = tensor;
// Get tensor type
final type = tensor.getProperty('type'.toJS).toString();
// Get tensor shape
final jsShape = tensor.getProperty('dims'.toJS) as JSObject;
final shapeLength = (jsShape.getProperty('length'.toJS) as JSNumber).toDartInt;
final shape = <int>[];
for (var i = 0; i < shapeLength; i++) {
final val = jsShape.getProperty(i.toString().toJS) as JSNumber;
shape.add(val.toDartInt);
}
// Format parameters like native platforms do
outputMap[name] = [valueId, _mapOrtTypeToSourceType(type), shape];
}
}
return outputMap;
} catch (e) {
if (e is PlatformException) {
rethrow;
}
throw PlatformException(code: "INFERENCE_ERROR", message: "Failed to run inference: $e", details: null);
}
}