addScalarMatrixGPU function

GPUTensor<Matrix> addScalarMatrixGPU(
  1. GPUTensor<Matrix> m,
  2. GPUTensor<Scalar> b,
  3. CommandBuffer tape, {
  4. GPUTensor<Matrix>? outTensor,
})

Appends a command for adding a scalar b to every element of a matrix m to tape.

Implementation

GPUTensor<Matrix> addScalarMatrixGPU(
    GPUTensor<Matrix> m, GPUTensor<Scalar> b, CommandBuffer tape,
    {GPUTensor<Matrix>? outTensor}) {
  int rows = m.shape[0];
  int cols = m.shape[1];

  List<int> shape = <int>[rows, cols];
  GPUTensor<Matrix> out = outTensor ?? GPUTensor<Matrix>.empty(shape);

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

  out.creator = GPUNode(
    <GPUTensor>[m, b],
    (CommandBuffer bTape) {
      bTape.putInt(OP_ADD_INTO);
      bTape.putString(out.gradId);
      bTape.putString(m.gradId);

      bTape.putInt(OP_SUM_REDUCE);
      bTape.putString(out.gradId);
      bTape.putString(b.gradId);
    },
    opName: 'addScalarToMatrixGPU',
    cost: rows * cols,
  );

  return out;
}