depthLogits method

Float32List depthLogits(
  1. Float32List hidden,
  2. List<int> codes, {
  3. void capture(
    1. String,
    2. Float32List
    )?,
  4. Iterable<int>? only,
})

One MaskGIT pass: -1 marks a masked codebook, others are known codes. Returns codebooks, vocabSize logits. No random sampling occurs here. With only, just those codebooks' rows are computed and the others stay zero: each row's arithmetic is the same either way.

Implementation

Float32List depthLogits(
  Float32List hidden,
  List<int> codes, {
  void Function(String, Float32List)? capture,
  Iterable<int>? only,
}) {
  _open();
  if (hidden.length != hiddenSize ||
      hidden.any((x) => !x.isFinite) ||
      codes.length != codebooks ||
      codes.any((c) => c < -1 || c >= vocabSize)) {
    throw ArgumentError('invalid depth input');
  }
  final rows = only?.toSet() ?? {for (var cb = 0; cb < codebooks; cb++) cb};
  if (rows.any((cb) => cb < 0 || cb >= codebooks)) {
    throw ArgumentError('invalid depth codebooks');
  }
  final h = _depth.h;
  final input = _linear('a.gen.code.proj_in', hidden, 1, bias: true);
  for (var cb = 0; cb < codebooks; cb++) {
    for (var j = 0; j < h; j++) {
      input[cb * h + j] += codes[cb] < 0
          ? _floats['a.gen.code.mask_embd']![j]
          : _heads[cb].at(codes[cb], j);
    }
  }
  capture?.call('input', Float32List.fromList(input));
  final output = _graph(_depth, input, codebooks, capture: capture);
  capture?.call('embedding', Float32List.fromList(output));
  final logits = Float32List(codebooks * vocabSize),
      norms = _floats['a.gen.code.head_norm.weight']!;
  for (final cb in rows) {
    final row = Float32List.fromList(output.sublist(cb * h, (cb + 1) * h));
    final norm = _norm(
      row,
      Float32List.sublistView(norms, cb * h, (cb + 1) * h),
      h,
    );
    logits.setAll(cb * vocabSize, _heads[cb].apply(norm, 1));
  }
  capture?.call('logits', Float32List.fromList(logits));
  return logits;
}