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
setProperty(jsInputs, name, tensor);
}
// Create run options if provided
JSObject? jsRunOptions;
if (runOptions != null && runOptions.isNotEmpty) {
jsRunOptions = createJSObject();
// Set run options if needed
if (runOptions.containsKey('logSeverityLevel')) {
setProperty(jsRunOptions, 'logSeverityLevel', runOptions['logSeverityLevel']);
}
if (runOptions.containsKey('logVerbosityLevel')) {
setProperty(jsRunOptions, 'logVerbosityLevel', runOptions['logVerbosityLevel']);
}
if (runOptions.containsKey('terminate')) {
setProperty(jsRunOptions, 'terminate', 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 (hasProperty(jsOutputs, name)) {
final tensor = getProperty(jsOutputs, name);
// Create a new OrtValue and store it
final valueId = '${DateTime.now().millisecondsSinceEpoch}_${math.Random().nextInt(10000)}';
_ortValues[valueId] = tensor;
// Get tensor type
final type = getProperty(tensor, 'type').toString();
// Get tensor shape
final jsShape = getProperty(tensor, 'dims');
final shapeLength = getProperty(jsShape, 'length') as int;
final shape = <int>[];
for (var i = 0; i < shapeLength; i++) {
shape.add(callMethod(jsShape, 'at', [i]) as int);
}
// 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);
}
}