applyInPlace method

void applyInPlace(
  1. Tensor2D t
)

Apply RoPE to t of shape (T, headDim). headDim must be even.

angles_j = pos * base^(-2j/headDim) pairs: (x_{2j}, x_{2j+1}) rotated by cos(angle), sin(angle)

Implementation

void applyInPlace(Tensor2D t) {
  final T = t.rows;
  final d = t.cols;
  if (d % 2 != 0) {
    throw ArgumentError('headDim must be even for RoPE');
  }
  final half = d ~/ 2;
  for (var i = 0; i < T; i++) {
    final baseIdx = i * d;
    for (var j = 0; j < half; j++) {
      final theta = i * math.pow(cfg.base, -2.0 * j / d).toDouble();
      final c = math.cos(theta);
      final s = math.sin(theta);
      final x0 = t.data[baseIdx + 2 * j];
      final x1 = t.data[baseIdx + 2 * j + 1];
      // Rotation: [x0', x1'] = [x0*c - x1*s, x0*s + x1*c]
      t.data[baseIdx + 2 * j] = x0 * c - x1 * s;
      t.data[baseIdx + 2 * j + 1] = x0 * s + x1 * c;
    }
  }
}