LayerShard.load constructor
LayerShard.load(})
Loads shard index of shards from model.safetensors, quantizing
to format every matrix whose name contains no entry of keepF32.
With Q8, matrices found in q8Cache are mapped instead of read and
quantized, and newly quantized ones are collected in q8Built.
Implementation
factory LayerShard.load(
String modelDir, {
required int index,
required int shards,
required int maxSeq,
required int maxChunk,
WeightFormat format = WeightFormat.f32,
List<String> keepF32 = const [],
Q8Cache? q8Cache,
Map<String, NativeMatrix>? q8Built,
}) {
if ((q8Cache != null || q8Built != null) && shards != 1) {
throw ArgumentError('the Q8 cache holds whole matrices: one shard only');
}
final c = Lfm3Config.load(modelDir);
final h = c.hiddenSize;
final hd = c.headDim;
final group = c.numHeads ~/ c.numKvHeads;
final st = SafetensorsFile.open('$modelDir/model.safetensors');
// Reads lazily so a cached Q8 matrix skips the safetensors read.
Linear linear(
String name,
int rows,
int cols,
Float32List Function() read,
) {
// The tied LM head stays Q8 under Q4 (see WeightFormat.q4).
final f = keepF32.any(name.contains)
? WeightFormat.f32
: format == WeightFormat.q4 && name == 'model.embed_tokens.weight'
? WeightFormat.q8
: format;
if (f.isNative) {
final cached = q8Cache?.matrix(name, rows, cols);
if (cached != null) return Linear.native(cached);
}
final made = Linear.from(read(), rows, cols, f);
if (made.native != null) q8Built?[name] = made.native!;
return made;
}
try {
final convs = <_ConvShard?>[];
final attentions = <_AttentionShard?>[];
final ffns = <_FfnShard>[];
final (c0, c1) = shareOf(h, index, shards, align: 4);
final (h0, h1) = shareOf(c.numHeads, index, shards);
final kv0 = h0 ~/ group;
final kv1 = h1 > h0 ? (h1 - 1) ~/ group + 1 : kv0;
final (f0, f1) = shareOf(c.intermediateSize, index, shards, align: 4);
for (var i = 0; i < c.numLayers; i++) {
final p = 'model.layers.$i.';
final type = c.layerTypes[i];
if (type == LayerType.conv) {
final name = '${p}conv.in_proj.weight';
final kernelAll = st.readF32('${p}conv.conv.weight', [
h,
1,
c.convKernel,
]);
convs.add(
_ConvShard(
c0,
c1,
linear(
name,
3 * (c1 - c0),
h,
() => readSlice(
st,
name,
3 * h,
h,
rowRanges: [
(c0, c1),
(h + c0, h + c1),
(2 * h + c0, 2 * h + c1),
],
),
),
Float32List.sublistView(
kernelAll,
c0 * c.convKernel,
c1 * c.convKernel,
),
linear(
'${p}conv.out_proj.weight',
h,
c1 - c0,
() => readSlice(
st,
'${p}conv.out_proj.weight',
h,
h,
colStart: c0,
colEnd: c1,
),
),
Float32List((c.convKernel - 1) * (c1 - c0)),
),
);
attentions.add(null);
} else {
final a = '${p}self_attn.';
final qDim = c.numHeads * hd;
attentions.add(
_AttentionShard(
h0: h0,
h1: h1,
kv0: kv0,
kv1: kv1,
q: linear(
'${a}q_proj',
(h1 - h0) * hd,
h,
() => readSlice(
st,
'${a}q_proj.weight',
qDim,
h,
rowRanges: [(h0 * hd, h1 * hd)],
),
),
k: linear(
'${a}k_proj',
(kv1 - kv0) * hd,
h,
() => readSlice(
st,
'${a}k_proj.weight',
c.kvDim,
h,
rowRanges: [(kv0 * hd, kv1 * hd)],
),
),
v: linear(
'${a}v_proj',
(kv1 - kv0) * hd,
h,
() => readSlice(
st,
'${a}v_proj.weight',
c.kvDim,
h,
rowRanges: [(kv0 * hd, kv1 * hd)],
),
),
gate: linear(
'${a}gate_proj',
h1 - h0,
h,
() => readSlice(
st,
'${a}gate_proj.weight',
c.numHeads,
h,
rowRanges: [(h0, h1)],
),
),
o: linear(
'${a}o_proj',
h,
(h1 - h0) * hd,
() => readSlice(
st,
'${a}o_proj.weight',
h,
qDim,
colStart: h0 * hd,
colEnd: h1 * hd,
),
),
qNorm: st.readF32('${a}q_norm.weight', [hd]),
kNorm: st.readF32('${a}k_norm.weight', [hd]),
keys: Float32List(maxSeq * (kv1 - kv0) * hd),
values: Float32List(maxSeq * (kv1 - kv0) * hd),
sliding: type == LayerType.slidingAttention,
),
);
convs.add(null);
}
final f = '${p}feed_forward.';
final fi = c.intermediateSize;
ffns.add(
_FfnShard(
linear(
'${f}w1',
f1 - f0,
h,
() =>
readSlice(st, '${f}w1.weight', fi, h, rowRanges: [(f0, f1)]),
),
linear(
'${f}w3',
f1 - f0,
h,
() =>
readSlice(st, '${f}w3.weight', fi, h, rowRanges: [(f0, f1)]),
),
linear(
'${f}w2',
h,
f1 - f0,
() => readSlice(
st,
'${f}w2.weight',
h,
fi,
colStart: f0,
colEnd: f1,
),
),
),
);
}
const embed = 'model.embed_tokens.weight';
final (v0, v1) = shareOf(c.vocabSize, index, shards, align: 2);
final lmHead = linear(
embed,
v1 - v0,
h,
() => readSlice(st, embed, c.vocabSize, h, rowRanges: [(v0, v1)]),
);
return LayerShard._(
c,
maxSeq,
maxChunk,
convs,
attentions,
ffns,
lmHead,
v0,
);
} finally {
st.close();
}
}