Lfm3AudioEncoder.load constructor

Lfm3AudioEncoder.load(
  1. String mmproj, {
  2. bool int8Inputs = false,
  3. bool nativeDownsampling = true,
})

Set nativeDownsampling to false to compare with the Dart audio stem.

Implementation

factory Lfm3AudioEncoder.load(
  String mmproj, {
  bool int8Inputs = false,
  bool nativeDownsampling = true,
}) {
  if (!NativeKernels.isLoaded) NativeKernels.load(null);
  final file = GgufFile.open(mmproj);
  final matrices = <String, NativeQ4Matrix>{};
  try {
    final c = Lfm3AudioEncoderConfig.fromGguf(file);
    final floats = <String, Float32List>{};
    for (final e in c.tensorShapes.entries) {
      final tensor = file.tensors[e.key]!;
      if (c.isQuantized(e.key)) {
        matrices[e.key] = NativeQ4Matrix.fromGgufBlocks(
          file.bytes(tensor),
          e.value[0],
          e.value[1],
        );
      } else {
        floats[e.key] = file.f32(tensor);
      }
    }
    return Lfm3AudioEncoder._(
      c,
      floats,
      matrices,
      int8Inputs,
      nativeDownsampling,
    );
  } catch (_) {
    for (final m in matrices.values) {
      m.free();
    }
    rethrow;
  } finally {
    file.close();
  }
}