Skip to main content

fonix 0.1.0-dev.1 copy "fonix: ^0.1.0-dev.1" to clipboard
fonix: ^0.1.0-dev.1 copied to clipboard

Safe, diagnosable ONNX Runtime inference for Dart and Flutter.

example/lib/main.dart

import 'dart:async';
import 'dart:io';

import 'package:flutter/widgets.dart';

import 'app.dart';
import 'src/android_smoke_channel.dart';
import 'src/android_xnnpack_qualification.dart';
import 'src/cpu_benchmark.dart';
import 'src/fonix_inference_backend.dart';
import 'src/reference_smoke.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (Platform.isAndroid && androidSmokeEnabled) {
    await _runAndroidSmoke(AndroidSmokeProfile.parse(androidSmokeProfileValue));
    return;
  }
  if (Platform.isIOS) {
    final ResidentReferenceChallenge? challenge;
    try {
      challenge = await readIosResidentReferenceChallenge();
    } on Object {
      stderr.writeln(residentReferenceActivationFailureDiagnostic);
      runApp(const SizedBox.shrink());
      return;
    }
    if (challenge != null) {
      runApp(const SizedBox.shrink());
      unawaited(
        _runResidentPackagedSmoke(challenge, pid).catchError((Object _) {
          stderr.writeln(residentReferencePublicationFailureDiagnostic);
        }),
      );
      return;
    }
  }
  if ((Platform.isMacOS || Platform.isLinux) &&
      desktopReferenceSmokeEnabled(
        isMacOS: Platform.isMacOS,
        isLinux: Platform.isLinux,
        environment: Platform.environment,
      )) {
    final int status = await _runPackagedSmoke();
    await stdout.flush();
    await stderr.flush();
    exit(status);
  }
  if ((Platform.isMacOS || Platform.isLinux) &&
      desktopCpuBenchmarkEnabled(
        isMacOS: Platform.isMacOS,
        isLinux: Platform.isLinux,
        environment: Platform.environment,
      )) {
    final int status = await _runPackagedCpuBenchmark();
    await stdout.flush();
    await stderr.flush();
    exit(status);
  }
  runApp(FonixReferenceApp(createBackend: FonixInferenceBackend.new));
}

Future<void> _runResidentPackagedSmoke(
  ResidentReferenceChallenge challenge,
  int processId,
) async {
  await runResidentReferenceSmoke(
    FonixInferenceBackend(),
    challenge: challenge,
    processId: processId,
    onPassed: (String line) => publishResidentReferenceLine(
      line,
      challenge: challenge,
      processId: processId,
    ),
    onFailed: (String line) => publishResidentReferenceLine(
      line,
      challenge: challenge,
      processId: processId,
    ),
  );
}

Future<int> _runPackagedSmoke() async {
  try {
    final ReferenceSmokeReceipt receipt = await runReferenceSmoke(
      FonixInferenceBackend(),
    );
    stdout.writeln('$referenceSmokeReceiptPrefix${receipt.toJsonString()}');
    return 0;
  } on Object catch (error) {
    stderr.writeln('Fonix reference smoke failed (${error.runtimeType}).');
    return 1;
  }
}

Future<int> _runPackagedCpuBenchmark() async {
  try {
    final CpuBenchmarkResult result = await runDesktopCpuBenchmark();
    stdout.writeln('$cpuBenchmarkResultPrefix${result.toJsonString()}');
    return 0;
  } on Object catch (error) {
    stderr.writeln('Fonix CPU benchmark failed (${error.runtimeType}).');
    return 1;
  }
}

Future<void> _runAndroidSmoke(AndroidSmokeProfile profile) async {
  const AndroidSmokeChannel channel = AndroidSmokeChannel();
  final String receipt;
  try {
    receipt = switch (profile) {
      AndroidSmokeProfile.cpu => (await runReferenceSmoke(
        FonixInferenceBackend(),
      )).toJsonString(),
      AndroidSmokeProfile.xnnpack =>
        (await runAndroidXnnpackQualification()).toJsonString(),
    };
  } on Object catch (error) {
    await channel.completeFailed(profile: profile, error: error);
    return;
  }
  await channel.completePassed(profile: profile, receipt: receipt);
}
0
likes
160
points
82
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Safe, diagnosable ONNX Runtime inference for Dart and Flutter.

Repository (GitHub)
View/report issues

Topics

#ffi #onnx #onnxruntime #machine-learning

License

GPL-3.0 (license)

Dependencies

code_assets, crypto, ffi, hooks, native_toolchain_c, path

More

Packages that depend on fonix