open static method
Maps path if it is a cache with key; otherwise returns null. A
cache that exists but cannot be mapped is an error rather than a miss:
a miss would quantize from model.safetensors, which a device bundle
(lfm3 pack) does not hold. So is a cache with this format's magic
whose header or matrices do not fit the file, such as a truncated
copy: it throws FormatException, since the kernels would otherwise
read past the mapping.
Implementation
static Q8Cache? open(String path, String key) {
final length = File(path).existsSync() ? File(path).lengthSync() : 0;
if (length < 16) return null;
final file =
MappedFile.open(path) ??
(throw StateError('mapping $path ($length bytes) failed'));
try {
if (utf8.decode(file.bytes(0, 8), allowMalformed: true) != _magic) {
file.release();
return null;
}
final headerLength = ByteData.sublistView(
file.bytes(8, 8),
).getUint64(0, Endian.little);
// A u64 above 2^63 reads as negative.
if (headerLength < 0 || headerLength > file.length - 16) {
throw FormatException('$path: header exceeds the file');
}
final header = jsonDecode(utf8.decode(file.bytes(16, headerLength)));
if (header is! Map<String, Object?>) {
throw FormatException('$path: header is not an object');
}
if (header['version'] != _version || header['key'] != key) {
file.release();
return null;
}
final tensors = header['tensors'];
if (tensors is! Map<String, Object?>) {
throw FormatException('$path: header lacks tensors');
}
final entries = <String, Map<String, Object?>>{};
for (final MapEntry(key: name, :value) in tensors.entries) {
if (value is! Map<String, Object?> || !_fits(value, file.length)) {
throw FormatException('$path: $name does not fit the file');
}
entries[name] = value;
}
return Q8Cache._(file, entries);
} on FormatException {
file.release();
rethrow;
}
}