matMulGPU function

GPUTensor<Matrix> matMulGPU(
  1. GPUTensor<Matrix> a,
  2. GPUTensor<Matrix> b,
  3. CommandBuffer tape, {
  4. GPUTensor<Matrix>? outTensor,
})

///////////////////////////////// Matrix Operations (200-299) /// ///////////////////////////////// Appends the commands for matrix multiplication of a with b to tape.

Implementation

/// Appends the commands for matrix multiplication of [a] with [b] to [tape].
GPUTensor<Matrix> matMulGPU(
    GPUTensor<Matrix> a, GPUTensor<Matrix> b, CommandBuffer tape,
    {GPUTensor<Matrix>? outTensor}) {
  int M = a.shape[0];
  int N = a.shape[1];
  int P = b.shape[1];

  // Instantly reserve VRAM based on shape, bypassing the Dart Heap
  GPUTensor<Matrix> out = outTensor ?? GPUTensor<Matrix>.empty(<int>[M, P]);

  tape.putInt(OP_MATMUL);
  tape.putString(a.id);
  tape.putString(b.id);
  tape.putString(out.id);
  tape.putBool(false);
  tape.putBool(false);
  tape.putFloat(1.0);
  tape.putFloat(0.0);
  tape.putBool(true); // <--- Tensor Cores ON for Forward Pass

  int cost = 2 * M * N * P;

  out.creator = GPUNode(
    <GPUTensor>[a, b],
    (CommandBuffer bTape) {
      bTape.putInt(OP_MATMUL);
      bTape.putString(out.gradId);
      bTape.putString(b.id);
      bTape.putString(a.gradId);
      bTape.putBool(false);
      bTape.putBool(true);
      bTape.putFloat(1.0);
      bTape.putFloat(1.0);
      bTape.putBool(true);

      bTape.putInt(OP_MATMUL);
      bTape.putString(a.id);
      bTape.putString(out.gradId);
      bTape.putString(b.gradId);
      bTape.putBool(true);
      bTape.putBool(false);
      bTape.putFloat(1.0);
      bTape.putFloat(1.0);
      bTape.putBool(true);
    },
    opName: 'matMulGPU',
    cost: cost,
  );

  return out;
}