importVlBackbone function
Writes a standalone language checkpoint into a new outDir.
The source must be a full BF16 LFM3-VL checkpoint with model.safetensors and tokenizer.json. Language tensor names become the engine's canonical names, with raw bytes unchanged. The original VL config is retained as vl_config.json for subsequent encoder integration. This does not enable image chat or substitute the text-chat model's weights for VL weights.
Required tensor names, shapes, dtypes and ranges are checked before any output is published. Output must not exist; a failed copy is cleaned up.
Implementation
void importVlBackbone(String modelDir, String outDir) {
final configFile = File('$modelDir/config.json');
final sourceConfig = configFile.readAsStringSync();
final json = jsonDecode(sourceConfig) as Map<String, Object?>;
final config = Lfm3VlConfig.fromJson(json);
final tokenizer = File('$modelDir/tokenizer.json');
if (!tokenizer.existsSync()) {
throw FileSystemException('VL checkpoint lacks tokenizer.json', modelDir);
}
final destination = Directory(outDir).absolute;
if (FileSystemEntity.typeSync(destination.path, followLinks: false) !=
FileSystemEntityType.notFound) {
throw FileSystemException('output already exists', destination.path);
}
final source = File('$modelDir/model.safetensors');
final st = SafetensorsFile.open(source.path);
Directory? staging;
try {
final shapes = lfm3LanguageTensorShapes(config.text);
final names = {
for (final name in shapes.keys)
Lfm3VlConfig.languageTensorName(name): name,
};
for (final entry in names.entries) {
final info = st.tensors[entry.key];
final expected = shapes[entry.value]!;
if (info == null || info.shape.join(',') != expected.join(',')) {
throw FormatException(
'VL tensor ${entry.key}: expected shape $expected',
);
}
if (info.dtype != 'BF16' &&
(info.dtype != 'F32' || entry.value == 'model.embed_tokens.weight')) {
throw FormatException(
'VL tensor ${entry.key}: unsupported dtype ${info.dtype}',
);
}
final bytes = info.numel * (info.dtype == 'BF16' ? 2 : 4);
if (info.begin < 0 ||
info.end - info.begin != bytes ||
st.dataStart + info.end > source.lengthSync()) {
throw FormatException('VL tensor ${entry.key}: invalid byte range');
}
}
for (final name in st.tensors.keys) {
if (name.startsWith(Lfm3VlConfig.languagePrefix) &&
!names.containsKey(name)) {
throw FormatException('unsupported VL language tensor $name');
}
}
destination.parent.createSync(recursive: true);
staging = destination.parent.createTempSync('.dianoia-vl-');
st.writeSubset(
'${staging.path}/model.safetensors',
(name, _) => names.containsKey(name),
rename: (name) => names[name]!,
);
File('${staging.path}/config.json').writeAsStringSync(
'${const JsonEncoder.withIndent(' ').convert(json['text_config'])}\n',
);
File('${staging.path}/vl_config.json').writeAsStringSync(sourceConfig);
tokenizer.copySync('${staging.path}/tokenizer.json');
writeVlBackboneIdentity(modelDir, staging.path);
staging.renameSync(destination.path);
staging = null;
} finally {
st.close();
if (staging != null) staging.deleteSync(recursive: true);
}
}