subtractGPU<T> function

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

Appends the commands for subtraction of two GPUTensors a and b of type T to the tape. Creates and allocates a Tensor to store the results.

Implementation

GPUTensor<T> subtractGPU<T>(GPUTensor<T> a, GPUTensor<T> b, CommandBuffer tape,
    {GPUTensor<T>? outTensor}) {
  // Correctly inherit shape to pre-allocate VRAM
  GPUTensor<T> out = outTensor ?? GPUTensor<T>.empty(a.shape);

  tape.putInt(OP_SUBTRACT);
  tape.putString(a.id);
  tape.putString(b.id);
  tape.putString(out.id);

  out.creator = GPUNode(
    <GPUTensor>[a, b],
    (CommandBuffer bTape) {
      // Forward: C = A - B
      // Backward: dA += dC, dB -= dC
      bTape.putInt(OP_ADD_INTO);
      bTape.putString(out.gradId);
      bTape.putString(a.gradId);

      bTape.putInt(OP_SUBTRACT_INTO);
      bTape.putString(out.gradId);
      bTape.putString(b.gradId);
    },
    opName: 'subtractGPU',
  );

  return out;
}