lfm3LanguageTensorShapes function

Map<String, List<int>> lfm3LanguageTensorShapes(
  1. Lfm3Config c
)

Canonical language tensor names and shapes, shared by import and packing.

Implementation

Map<String, List<int>> lfm3LanguageTensorShapes(Lfm3Config c) {
  final h = c.hiddenSize;
  final shapes = <String, List<int>>{
    'model.embed_tokens.weight': [c.vocabSize, h],
    'model.embedding_norm.weight': [h],
  };
  for (var i = 0; i < c.numLayers; i++) {
    final p = 'model.layers.$i.';
    for (final norm in [
      'operator_norm',
      'ffn_norm',
      if (c.sandwichNorm) ...['post_operator_norm', 'post_ffn_norm'],
    ]) {
      shapes['$p$norm.weight'] = [h];
    }
    for (final name in ['w1', 'w3']) {
      shapes['${p}feed_forward.$name.weight'] = [c.intermediateSize, h];
    }
    shapes['${p}feed_forward.w2.weight'] = [h, c.intermediateSize];
    if (c.layerTypes[i] == LayerType.conv) {
      shapes['${p}conv.in_proj.weight'] = [3 * h, h];
      shapes['${p}conv.out_proj.weight'] = [h, h];
      shapes['${p}conv.conv.weight'] = [h, 1, c.convKernel];
    } else {
      final a = '${p}self_attn.';
      shapes['${a}q_proj.weight'] = [c.numHeads * c.headDim, h];
      shapes['${a}k_proj.weight'] = [c.kvDim, h];
      shapes['${a}v_proj.weight'] = [c.kvDim, h];
      shapes['${a}gate_proj.weight'] = [c.numHeads, h];
      shapes['${a}o_proj.weight'] = [h, c.numHeads * c.headDim];
      shapes['${a}q_norm.weight'] = [c.headDim];
      shapes['${a}k_norm.weight'] = [c.headDim];
    }
  }
  return shapes;
}