NativeQ8Matrix.quantizeRows constructor

NativeQ8Matrix.quantizeRows(
  1. int rows,
  2. int cols,
  3. void fill(
    1. int first,
    2. int count,
    3. Float32List out
    ), {
  4. int chunk = 1024,
})

Quantizes a matrix too large to hold in float32 at once: fill writes count rows starting at row first into its list, chunk at a time.

Implementation

factory NativeQ8Matrix.quantizeRows(
  int rows,
  int cols,
  void Function(int first, int count, Float32List out) fill, {
  int chunk = 1024,
}) {
  final qs = NativeKernels.allocate<Int8>(rows * cols);
  final scales = NativeKernels.allocate<Float>(rows * cols ~/ 32 * 4);
  try {
    final buffer = Float32List(chunk * cols);
    for (var first = 0; first < rows; first += chunk) {
      final count = rows - first < chunk ? rows - first : chunk;
      fill(first, count, buffer);
      _quantizeQ8(
        buffer.address,
        qs + first * cols,
        scales + first * cols ~/ 32,
        count,
        cols,
      );
    }
  } catch (_) {
    _free(qs.cast());
    _free(scales.cast());
    rethrow;
  }
  return NativeQ8Matrix._(rows, cols, qs, scales);
}