loadIntoTensors method

void loadIntoTensors(
  1. Map<String, GPUTensor> modelTensors
)

Extracts binary float values from the buffer for all matching keys in modelTensors, automatically transposes 2D matrices if row and column dimensions are inverted, and uploads the resulting values directly to VRAM via GPUTensor.pushData.

Implementation

void loadIntoTensors(Map<String, GPUTensor> modelTensors) {
  List<String> keys = modelTensors.keys.toList();

  for (int i = 0; i < keys.length; i = i + 1) {
    String key = keys[i];

    if (header.containsKey(key) == false) {
      continue;
    }

    GPUTensor tensor = modelTensors[key]!;

    Map<String, dynamic> tensorMeta = header[key];
    List<dynamic> jsonShape = tensorMeta['shape'];
    List<dynamic> offsets = tensorMeta['data_offsets'];

    int startOffset = offsets[0];
    int endOffset = offsets[1];

    int absoluteStart = bufferOffset + startOffset;
    int byteLength = endOffset - startOffset;
    int floatCount = byteLength ~/ 4;

    ByteData tensorData = ByteData.sublistView(
        rawBytes, absoluteStart, absoluteStart + byteLength);
    List<double> values = <double>[];

    for (int j = 0; j < floatCount; j = j + 1) {
      values.add(tensorData.getFloat32(j * 4, Endian.little));
    }

    if (tensor.shape.length == 2 && jsonShape.length == 2) {
      int hfRows = jsonShape[0];
      int hfCols = jsonShape[1];
      int engineRows = tensor.shape[0];
      int engineCols = tensor.shape[1];

      if (hfRows == engineCols && hfCols == engineRows) {
        List<double> transposed = <double>[];
        for (int k = 0; k < floatCount; k = k + 1) {
          transposed.add(0.0);
        }

        for (int r = 0; r < hfRows; r = r + 1) {
          for (int c = 0; c < hfCols; c = c + 1) {
            int srcIndex = (r * hfCols) + c;
            int destIndex = (c * hfRows) + r;
            transposed[destIndex] = values[srcIndex];
          }
        }
        values = transposed;
      }
    }

    tensor.pushData(values);
  }
}