loadVisionEncoderGguf function

Lfm3VisionEncoder loadVisionEncoderGguf(
  1. String path,
  2. Lfm3VlConfig config, {
  3. bool nativeQ4 = true,
})

Loads the CPU vision encoder straight from an mmproj GGUF, without writing an intermediate safetensors file. Q4_0 linear weights stay native Q4 blocks, multiplied with float inputs, unless nativeQ4 is false; then every weight is widened to float32 on the Dart heap.

Implementation

Lfm3VisionEncoder loadVisionEncoderGguf(
  String path,
  Lfm3VlConfig config, {
  bool nativeQ4 = true,
}) {
  if (nativeQ4 && !NativeKernels.isLoaded) NativeKernels.load(null);
  final gguf = GgufFile.open(path);
  final native = <String, NativeMatrix>{};
  try {
    final weights = _readVisionGguf(gguf, config, nativeQ4 ? native : null);
    return Lfm3VisionEncoder.fromWeights(config, weights, native: native);
  } catch (_) {
    for (final m in native.values) {
      m.free();
    }
    rethrow;
  } finally {
    gguf.close();
  }
}