softmaxMatrixGPU function

GPUTensor<Matrix> softmaxMatrixGPU(
  1. GPUTensor<Matrix> m,
  2. CommandBuffer tape, {
  3. GPUTensor<Matrix>? outTensor,
})

Appends commands for applying the softmax function on every element of matrix m to tape.

Implementation

GPUTensor<Matrix> softmaxMatrixGPU(GPUTensor<Matrix> m, 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_SOFTMAX_FORWARD);
  tape.putString(m.id);
  tape.putString(out.id);

  out.creator = GPUNode(
    [m],
    (CommandBuffer bTape) {
      bTape.putInt(OP_SOFTMAX_BACKWARD);
      bTape.putString(out.gradId);
      bTape.putString(out.id);
      bTape.putString(m.gradId);
    },
    opName: 'softmax_matrixGPU',
    cost: numRows * numCols * 3,
  );

  return out;
}