Lfm3VisionEncoder.fromWeights constructor

Lfm3VisionEncoder.fromWeights(
  1. Lfm3VlConfig config,
  2. Map<String, Float32List> weights, {
  3. Map<String, NativeMatrix> native = const {},
})

Uses already widened weights, keyed by the checkpoint names of lfm3VisionTensorShapes. Every tensor must be in exactly one of weights and native; native holds only 2D linear weights, and the encoder takes ownership of them.

Implementation

factory Lfm3VisionEncoder.fromWeights(
  Lfm3VlConfig config,
  Map<String, Float32List> weights, {
  Map<String, NativeMatrix> native = const {},
}) {
  final shapes = lfm3VisionTensorShapes(config);
  if (weights.length + native.length != shapes.length) {
    throw ArgumentError('expected ${shapes.length} vision tensors');
  }
  for (final e in shapes.entries) {
    final values = weights[e.key], matrix = native[e.key];
    final ok = values != null
        ? matrix == null && values.length == e.value.fold(1, (a, b) => a * b)
        : matrix != null &&
              e.value.length == 2 &&
              !e.key.endsWith('patchifier.weight') &&
              matrix.rows == e.value[0] &&
              matrix.cols == e.value[1];
    if (!ok) {
      throw ArgumentError('vision tensor ${e.key} differs from its shape');
    }
  }
  return Lfm3VisionEncoder._(config, Map.unmodifiable(weights), native);
}