init static method

Future<void> init({
  1. Uint8List? modelBytes,
  2. String? modelPath,
  3. OrtSessionOptions? options,
  4. int? intraOpNumThreads,
})

Initialize ONNX model on a background worker isolate.

Provide either modelBytes (loads from memory) or modelPath (loads from file). modelPath is recommended for memory optimization.

intraOpNumThreads configures ONNX intra-op parallelism. If omitted, the ONNX default is used.

The options parameter is retained for backward compatibility but is ignoredOrtSessionOptions cannot be transferred across isolate boundaries. Use intraOpNumThreads instead.

Implementation

static Future<void> init({
  Uint8List? modelBytes,
  String? modelPath,
  OrtSessionOptions? options,
  int? intraOpNumThreads,
}) async {
  if (options != null && intraOpNumThreads == null) {
    debugPrint(
      '[EmbeddingService] Warning: OrtSessionOptions cannot be transferred to '
      'the background worker isolate. Use intraOpNumThreads instead.',
    );
  }

  // Tear down any previous worker.
  await _shutdownWorker();

  // Spawn a fresh worker isolate.
  final bootstrapPort = ReceivePort();
  _workerIsolate = await Isolate.spawn(
    _workerEntryPoint,
    bootstrapPort.sendPort,
  );

  // First message from the worker is its own SendPort.
  _workerSendPort = await bootstrapPort.first as SendPort;
  bootstrapPort.close();

  // Send the init command and wait for acknowledgement.
  final replyPort = ReceivePort();
  _workerSendPort!.send({
    'cmd': 'init',
    'modelPath': modelPath,
    'modelBytes': modelBytes,
    'intraOpNumThreads': intraOpNumThreads,
    'replyPort': replyPort.sendPort,
  });

  final result = await replyPort.first;
  replyPort.close();

  if (result is Map && result['error'] != null) {
    await _shutdownWorker();
    throw Exception(result['error']);
  }
}