addMatrixAndVectorGPU function

GPUTensor<Matrix> addMatrixAndVectorGPU(
  1. GPUTensor<Matrix> m,
  2. GPUTensor<Vector> v,
  3. CommandBuffer tape, {
  4. GPUTensor<Matrix>? outTensor,
})

Appends a command for broadcast add a vector v to tape.

Implementation

GPUTensor<Matrix> addMatrixAndVectorGPU(
    GPUTensor<Matrix> m, GPUTensor<Vector> v, CommandBuffer tape,
    {GPUTensor<Matrix>? outTensor}) {
  int numRows = m.shape[0];
  int numCols = m.shape[1];

  GPUTensor<Matrix> out =
      outTensor ?? GPUTensor<Matrix>.empty(<int>[numRows, numCols]);

  tape.putInt(OP_BROADCAST_ADD);
  tape.putString(m.id);
  tape.putString(v.id);
  tape.putString(out.id);

  out.creator = GPUNode(
    [m, v],
    (CommandBuffer bTape) {
      bTape.putInt(OP_ADD_INTO);
      bTape.putString(out.gradId);
      bTape.putString(m.gradId);

      bTape.putInt(OP_SUM_REDUCE_COLUMNS);
      bTape.putString(out.gradId);
      bTape.putString(v.gradId);
    },
    opName: 'addMatrixAndVectorGPU',
    cost: numRows * numCols,
  );

  return out;
}