embeddingLookupGPU function

GPUTensor<Matrix> embeddingLookupGPU(
  1. GPUTensor<Vector> indices,
  2. GPUTensor<Matrix> weights,
  3. CommandBuffer tape, {
  4. GPUTensor<Matrix>? outTensor,
})

Acts as a high performance lookup of rows in a given matrix weights via their row indices indices. Can be used for fast odering of elements. The operation is appended to tape for execution.

Implementation

GPUTensor<Matrix> embeddingLookupGPU(
    GPUTensor<Vector> indices, GPUTensor<Matrix> weights, CommandBuffer tape,
    {GPUTensor<Matrix>? outTensor}) {
  int numIndices = indices.shape[0];
  int embeddingDim = weights.shape[1];

  GPUTensor<Matrix> out =
      outTensor ?? GPUTensor<Matrix>.empty([numIndices, embeddingDim]);

  tape.putInt(OP_EMBEDDING_FORWARD);
  tape.putString(indices.id);
  tape.putString(weights.id);
  tape.putString(out.id);

  out.creator = GPUNode(
    [weights],
    (CommandBuffer bTape) {
      bTape.putInt(OP_EMBEDDING_BACKWARD);
      bTape.putString(out.gradId);
      bTape.putString(indices.id);
      bTape.putString(weights.gradId);
    },
    opName: 'embedding_lookup_gpu',
    cost: numIndices * embeddingDim,
  );

  return out;
}