backward method

void backward(
  1. CommandBuffer backwardTape, {
  2. bool fillOnes = true,
})

Staring point of gradient calculation. The network of GPUNodes and GPUTensors traverses in topological oder over all GPUTensors writing their given commands to calculate the gradients to backwardsTape. To actually calculate the values this tape is required to be executed via GPUEngine.run(backwardTape). If fillOnes is set to true, it pushes a command to fill the gradient of this tensor with 1.0.

Implementation

void backward(CommandBuffer backwardTape, {bool fillOnes = true}) {
  if (fillOnes == true) {
    backwardTape.putInt(OP_FILL);
    backwardTape.putString('${id}_grad');
    backwardTape.putFloat(1.0);
  }

  if (creator == null) return;

  List<GPUNode> topo = [];
  Set<GPUNode> visited = {};

  void buildTopo(GPUNode? node) {
    if (node == null || visited.contains(node)) return;
    visited.add(node);
    for (int i = 0; i < node.inputs.length; i = i + 1) {
      GPUTensor inputTensor = node.inputs[i];
      if (inputTensor.creator != null) {
        buildTopo(inputTensor.creator);
      }
    }
    topo.add(node);
  }

  buildTopo(creator);

  for (int i = topo.length - 1; i >= 0; i = i - 1) {
    topo[i].backwardFn(backwardTape);
  }
}