embeddingLookupGPU function

GPUTensor<Matrix> embeddingLookupGPU(
  1. GPUTensor<Vector> indices,
  2. GPUTensor<Matrix> weights,
  3. CommandBuffer tape
)

Implementation

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

  GPUTensor<Matrix> out = 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.id}_grad');
      bTape.putString(indices.id);
      bTape.putString('${weights.id}_grad');
    },
    opName: 'embedding_lookup_gpu',
    cost: numIndices * embeddingDim,
  );

  return out;
}