buildMarkovTableGPU function

GPUTensor<Matrix> buildMarkovTableGPU(
  1. GPUTensor<Vector> sequence,
  2. int order,
  3. int numStates,
  4. CommandBuffer tape,
)

Implementation

GPUTensor<Matrix> buildMarkovTableGPU(
    GPUTensor<Vector> sequence,
    int order,
    int numStates,
    CommandBuffer tape) {

  int numHistories = math.pow(numStates, order).toInt();

  // 1. Allocate VRAM for count and probability tables
  GPUTensor<Matrix> countTable = GPUTensor<Matrix>.empty(<int>[numHistories, numStates]);
  GPUTensor<Matrix> probTable = GPUTensor<Matrix>.empty(<int>[numHistories, numStates]);

  // 2. Zero out the count table (OP_FILL)
  tape.putInt(OP_FILL);
  tape.putString(countTable.id);
  tape.putFloat(0.0);

  // 3. Count Transitions
  tape.putInt(OP_MARKOV_COUNT);
  tape.putString(sequence.id);
  tape.putString(countTable.id);
  tape.putInt(order);
  tape.putInt(numStates);

  // 4. Normalize Counts to Probabilities
  tape.putInt(OP_MARKOV_NORMALIZE);
  tape.putString(countTable.id);
  tape.putString(probTable.id);
  tape.putInt(numHistories);
  tape.putInt(numStates);

  // Note: Markov chains in this format don't have gradients.
  probTable.creator = GPUNode(
      <GPUTensor>[sequence],
          (CommandBuffer bTape) {}, // Empty backward pass
      opName: 'buildMarkovTableGPU'
  );

  return probTable;
}