embeddingLookupBatchGPU function

GPUTensor<Tensor3D> embeddingLookupBatchGPU(
  1. GPUTensor<Matrix> batchIndices,
  2. GPUTensor<Matrix> weights,
  3. CommandBuffer tape
)

Implementation

GPUTensor<Tensor3D> embeddingLookupBatchGPU(GPUTensor<Matrix> batchIndices, GPUTensor<Matrix> weights, CommandBuffer tape) {
  int batchSize = batchIndices.shape[0];
  int sequenceLength = batchIndices.shape[1];
  int embeddingDim = weights.shape[1];

  GPUTensor<Tensor3D> out = GPUTensor<Tensor3D>.empty([batchSize, sequenceLength, embeddingDim]);

  tape.putInt(OP_EMBEDDING_FORWARD);
  tape.putString(batchIndices.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(batchIndices.id);
      bTape.putString('${weights.id}_grad');
    },
    opName: 'embedding_lookup_batch_gpu',
    cost: batchSize * sequenceLength * embeddingDim,
  );

  return out;
}