maxPool2dGPU function

GPUTensor<Matrix> maxPool2dGPU(
  1. GPUTensor<Matrix> input,
  2. int poolSize,
  3. int stride,
  4. CommandBuffer tape, {
  5. GPUTensor<Matrix>? outTensor,
  6. GPUTensor<Matrix>? indicesTensor,
})

Slides a pool of size poolSizexpoolSize over matrix input. The resulting matrix consists of the maximum elements in each window. The value of stride tells how big the step size of the sliding window is in x and y direction. The operations are appended to tape for execution.

Implementation

GPUTensor<Matrix> maxPool2dGPU(
    GPUTensor<Matrix> input, int poolSize, int stride, CommandBuffer tape,
    {GPUTensor<Matrix>? outTensor, GPUTensor<Matrix>? indicesTensor}) {
  int inputHeight = input.shape[0];
  int inputWidth = input.shape[1];
  int outputHeight = (inputHeight - poolSize) ~/ stride + 1;
  int outputWidth = (inputWidth - poolSize) ~/ stride + 1;

  GPUTensor<Matrix> out =
      outTensor ?? GPUTensor<Matrix>.empty(<int>[outputHeight, outputWidth]);
  GPUTensor<Matrix> indices = indicesTensor ??
      GPUTensor<Matrix>.empty(<int>[outputHeight, outputWidth]);
  out.subMap["indeces"] = indices;

  tape.putInt(OP_MAX_POOL_2D_FORWARD);
  tape.putString(input.id);
  tape.putString(out.id);
  tape.putString(indices.id);
  tape.putInt(poolSize);
  tape.putInt(stride);

  out.creator = GPUNode(
    [input],
    (CommandBuffer bTape) {
      bTape.putInt(OP_MAX_POOL_2D_BACKWARD);
      bTape.putString(out.gradId);
      bTape.putString(indices.id);
      bTape.putString(input.gradId);
    },
    opName: 'maxPool2dGPU',
    cost: inputHeight * inputWidth,
  );

  return out;
}