buildVlBackboneCache function

void buildVlBackboneCache(
  1. String backboneDir, {
  2. required WeightFormat weights,
})

Quantizes one matrix at a time without constructing an inference model. Each native allocation is released immediately after it is written.

Implementation

void buildVlBackboneCache(String backboneDir, {required WeightFormat weights}) {
  if (!weights.isNative) throw ArgumentError('expected Q8 or Q4');
  final shapes = lfm3LanguageTensorShapes(Lfm3Config.load(backboneDir));
  final tensors = <String, (String, List<int>)>{};
  for (final e in shapes.entries.where((e) => e.value.length == 2)) {
    final name =
        e.key.contains('.self_attn.') || e.key.contains('.feed_forward.')
        ? e.key.substring(0, e.key.length - '.weight'.length)
        : e.key;
    tensors[name] = (e.key, e.value);
  }
  String format(String name) =>
      name == 'model.embed_tokens.weight' ? 'q8' : weights.name;
  final st = SafetensorsFile.open('$backboneDir/model.safetensors');
  try {
    Q8Cache.writeEach(
      '$backboneDir/lfm3-${weights.name}.cache',
      Q8Cache.keyFor(backboneDir, weights: weights.name),
      [
        for (final e in tensors.entries)
          (e.key, format(e.key), e.value.$2[0], e.value.$2[1]),
      ],
      (name) {
        final (tensor, shape) = tensors[name]!;
        if (format(name) == 'q8') {
          return NativeQ8Matrix.quantizeRows(
            shape[0],
            shape[1],
            (first, count, out) => out.setRange(
              0,
              count * shape[1],
              st.readF32(tensor, shape, rowStart: first, rowEnd: first + count),
            ),
          );
        }
        return NativeQ4Matrix.quantize(
          st.readF32(tensor, shape),
          shape[0],
          shape[1],
        );
      },
      done: (m) => (m as NativeMatrix).free(),
    );
  } finally {
    st.close();
  }
}