buildMarkovTableGPU function
Constructs a Markov chain probability transition table from a given state sequence.
Calculates the frequencies of state transitions based on the specified Markov order and numStates,
and normalizes these counts to output a probability matrix of shape [numStates^order, numStates]. This Table does not have a gradient calculation.
The operations are appended to tape for execution.
Implementation
GPUTensor<Matrix> buildMarkovTableGPU(
GPUTensor<Vector> sequence, int order, int numStates, CommandBuffer tape,
{GPUTensor<Matrix>? outTensor, GPUTensor<Matrix>? probTableTensor}) {
int numHistories = math.pow(numStates, order).toInt();
// 1. Allocate VRAM for count and probability tables
GPUTensor<Matrix> countTable =
outTensor ?? GPUTensor<Matrix>.empty(<int>[numHistories, numStates]);
GPUTensor<Matrix> probTable = probTableTensor ??
GPUTensor<Matrix>.empty(<int>[numHistories, numStates]);
probTable.subMap["countTable"] = countTable;
// 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);
probTable.creator = GPUNode(
<GPUTensor>[sequence], (CommandBuffer bTape) {}, // Empty backward pass
opName: 'buildMarkovTableGPU');
return probTable;
}