keyFor static method

String keyFor(
  1. String modelDir, {
  2. String weights = 'q8',
})

Fingerprint of the checkpoint in modelDir and of this cache format for weights (q8 or q4).

Implementation

static String keyFor(String modelDir, {String weights = 'q8'}) {
  final st = File('$modelDir/model.safetensors').openSync();
  try {
    final lengthBytes = st.readSync(8);
    final headerLength = ByteData.sublistView(
      lengthBytes,
    ).getUint64(0, Endian.little);
    var hash = _fnv(_fnvOffset, lengthBytes);
    hash = _fnv(hash, st.readSync(headerLength));
    hash = _fnv(hash, utf8.encode('${st.lengthSync()}'));
    hash = _fnv(hash, File('$modelDir/config.json').readAsBytesSync());
    // Q8 keeps its original key text, so existing Q8 caches stay valid.
    hash = _fnv(hash, utf8.encode('$_magic v$_version $weights block 32'));
    return hash.toUnsigned(64).toRadixString(16).padLeft(16, '0');
  } finally {
    st.closeSync();
  }
}