Class ComputeGraph
java.lang.Object
io.github.kirstenali.deepj.tensor.ComputeGraph
Collects GPU operations lazily and flushes them as a single command buffer.
Operations are recorded as a flat int[] command stream. Buffer IDs reference
persistent GPU-side buffers managed by a GpuRuntime. Data stays GPU-resident
between ops -- only uploaded at graph entry and downloaded on materialization.
This class is backend-agnostic: Metal, CUDA, Vulkan, etc. are all supported
by supplying the appropriate GpuRuntime implementation.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final int -
Constructor Summary
ConstructorsConstructorDescriptionComputeGraph(GpuRuntime runtime) Create a ComputeGraph backed by the given GPU runtime. -
Method Summary
Modifier and TypeMethodDescriptionvoidbindTensorToBuffer(Tensor t, GpuBuffer buf) Rebind an existing tensor to a GPU buffer and track ownership for lifecycle management.Create a Tensor backed by a GpuBuffer.Ensure a tensor has a GpuBuffer.voidflush()Flush all recorded ops to the GPU as a single command buffer.booleanisEmpty()voidMaterialize a tensor: flush pending ops if needed, then download GPU data to CPU.newOutputBuffer(int rows, int cols) Allocate a new output buffer (result of a GPU op).voidrecordAdamWUpdate(GpuBuffer w, GpuBuffer g, GpuBuffer mt, GpuBuffer vt, float lr, float beta1, float beta2, float eps, float weightDecay, float bc1, float bc2, int n) Record in-place AdamW update: [OP_ADAMW_UPDATE, wId, gId, mtId, vtId, lrBits, beta1Bits, beta2Bits, epsBits, weightDecayBits, bc1Bits, bc2Bits, n]voidrecordBinary(int opCode, GpuBuffer a, GpuBuffer b, GpuBuffer out) Record a binary element-wise op: [opCode, aId, bId, outId, n]voidrecordClamp(GpuBuffer in, GpuBuffer out, float min, float max) Record clamp: [OP_CLAMP, inId, outId, minBits, maxBits, n]voidrecordColBroadcast(int opCode, GpuBuffer a, GpuBuffer colVec, GpuBuffer out, int rows, int cols) Record col broadcast: [opCode, aId, colVecId, outId, rows, cols]voidrecordCrossEntropyGradient(GpuBuffer logits, GpuBuffer targets, GpuBuffer out, int rows, int cols) Record cross-entropy gradient: [OP_CROSS_ENTROPY_GRADIENT, logitsId, targetsId, outId, rows, cols]voidrecordCrossEntropyLoss(GpuBuffer logits, GpuBuffer targets, GpuBuffer out, int rows, int cols) Record cross-entropy row losses: [OP_CROSS_ENTROPY_LOSS, logitsId, targetsId, outId, rows, cols]voidrecordLayerNormBackward(GpuBuffer dXHat, GpuBuffer xHat, GpuBuffer std, GpuBuffer out, int rows, int cols) Record layer norm backward: [OP_LAYERNORM_BACKWARD, dXHatId, xHatId, stdId, outId, rows, cols]voidrecordMatmul(GpuBuffer a, GpuBuffer b, GpuBuffer out, int m, int n, int k) Record matmul: [OP_MATMUL, aId, bId, outId, m, n, k]voidrecordMultiplyScalar(GpuBuffer in, GpuBuffer out, float scalar) Record scalar multiply: [OP_MULTIPLY_SCALAR, inId, outId, scalarBits, n]voidRecord pow: [OP_POW, inId, outId, exponentBits, n]voidrecordReduction(int opCode, GpuBuffer in, GpuBuffer out, int rows, int cols) Record row/col reduction: [opCode, inId, outId, rows, cols]voidrecordRowBroadcast(int opCode, GpuBuffer a, GpuBuffer rowVec, GpuBuffer out, int rows, int cols) Record row broadcast: [opCode, aId, rowVecId, outId, rows, cols]voidrecordScalarUnary(int opCode, GpuBuffer in, GpuBuffer out, float scalar) Record scalar add/divide: [opCode, inId, outId, scalarBits, n]voidrecordScatterAddRows(GpuBuffer target, GpuBuffer indices, GpuBuffer grad, int targetRows, int targetCols, int nIndices) Record scatter-add-rows: [OP_SCATTER_ADD_ROWS, targetId, indicesId, gradId, targetRows, targetCols, nIdx]voidrecordScatterAddRowsAtomic(GpuBuffer target, GpuBuffer indices, GpuBuffer grad, int targetRows, int targetCols, int nIndices) Record scatter-add-rows (atomic): [OP_SCATTER_ADD_ROWS_ATOMIC, targetId, indicesId, gradId, targetRows, targetCols, nIdx]voidrecordSoftmaxBackward(GpuBuffer gradOutput, GpuBuffer softmaxOut, GpuBuffer out, int rows, int cols) Record softmax backward: [OP_SOFTMAX_BACKWARD, gradId, softmaxId, outId, rows, cols]voidrecordSoftmaxRows(GpuBuffer in, GpuBuffer out, int rows, int cols) Record softmax rows: [OP_SOFTMAX_ROWS, inId, outId, rows, cols]voidrecordSumAbs(GpuBuffer in, GpuBuffer out, int rows, int cols) Record sum-abs row reduction: [OP_SUM_ABS, inId, outId, rows, cols]voidrecordSumScalar(GpuBuffer in, GpuBuffer out, int rows, int cols) Record scalar sum reduction: [OP_SUM_SCALAR, inId, outId, rows, cols]voidrecordTranspose(GpuBuffer in, GpuBuffer out, int rows, int cols) Record transpose: [OP_TRANSPOSE, inId, outId, rows, cols]voidrecordUnary(int opCode, GpuBuffer in, GpuBuffer out) Record a unary op: [opCode, inId, outId, n]voidRelease all GPU buffers and reset the graph completely.
-
Field Details
-
OP_ADD
public static final int OP_ADD- See Also:
-
OP_SUBTRACT
public static final int OP_SUBTRACT- See Also:
-
OP_MULTIPLY
public static final int OP_MULTIPLY- See Also:
-
OP_DIVIDE
public static final int OP_DIVIDE- See Also:
-
OP_MATMUL
public static final int OP_MATMUL- See Also:
-
OP_MULTIPLY_SCALAR
public static final int OP_MULTIPLY_SCALAR- See Also:
-
OP_SQRT
public static final int OP_SQRT- See Also:
-
OP_NEG
public static final int OP_NEG- See Also:
-
OP_EXP
public static final int OP_EXP- See Also:
-
OP_LOG
public static final int OP_LOG- See Also:
-
OP_TANH
public static final int OP_TANH- See Also:
-
OP_SIGMOID
public static final int OP_SIGMOID- See Also:
-
OP_RELU
public static final int OP_RELU- See Also:
-
OP_RELU_BACKWARD
public static final int OP_RELU_BACKWARD- See Also:
-
OP_GELU
public static final int OP_GELU- See Also:
-
OP_GELU_BACKWARD
public static final int OP_GELU_BACKWARD- See Also:
-
OP_SOFTMAX_ROWS
public static final int OP_SOFTMAX_ROWS- See Also:
-
OP_SOFTMAX_BACKWARD
public static final int OP_SOFTMAX_BACKWARD- See Also:
-
OP_LAYERNORM_BACKWARD
public static final int OP_LAYERNORM_BACKWARD- See Also:
-
OP_ADAMW_UPDATE
public static final int OP_ADAMW_UPDATE- See Also:
-
OP_ADD_SCALAR
public static final int OP_ADD_SCALAR- See Also:
-
OP_DIVIDE_SCALAR
public static final int OP_DIVIDE_SCALAR- See Also:
-
OP_TRANSPOSE
public static final int OP_TRANSPOSE- See Also:
-
OP_ADD_ROW_VECTOR
public static final int OP_ADD_ROW_VECTOR- See Also:
-
OP_ADD_BROADCAST_COLS
public static final int OP_ADD_BROADCAST_COLS- See Also:
-
OP_SUBTRACT_BROADCAST_COLS
public static final int OP_SUBTRACT_BROADCAST_COLS- See Also:
-
OP_DIVIDE_BROADCAST_COLS
public static final int OP_DIVIDE_BROADCAST_COLS- See Also:
-
OP_MULTIPLY_BROADCAST_ROWS
public static final int OP_MULTIPLY_BROADCAST_ROWS- See Also:
-
OP_SUM_ROWS
public static final int OP_SUM_ROWS- See Also:
-
OP_MEAN_ALONG_ROWS
public static final int OP_MEAN_ALONG_ROWS- See Also:
-
OP_VARIANCE_ALONG_ROWS
public static final int OP_VARIANCE_ALONG_ROWS- See Also:
-
OP_MULTIPLY_BROADCAST_COLS
public static final int OP_MULTIPLY_BROADCAST_COLS- See Also:
-
OP_SUM_ALONG_ROWS
public static final int OP_SUM_ALONG_ROWS- See Also:
-
OP_MAX_ALONG_ROWS
public static final int OP_MAX_ALONG_ROWS- See Also:
-
OP_CLAMP
public static final int OP_CLAMP- See Also:
-
OP_POW
public static final int OP_POW- See Also:
-
OP_SCATTER_ADD_ROWS
public static final int OP_SCATTER_ADD_ROWS- See Also:
-
OP_SUM_ABS
public static final int OP_SUM_ABS- See Also:
-
OP_CROSS_ENTROPY_LOSS
public static final int OP_CROSS_ENTROPY_LOSS- See Also:
-
OP_CROSS_ENTROPY_GRADIENT
public static final int OP_CROSS_ENTROPY_GRADIENT- See Also:
-
OP_SUM_SCALAR
public static final int OP_SUM_SCALAR- See Also:
-
OP_SCATTER_ADD_ROWS_ATOMIC
public static final int OP_SCATTER_ADD_ROWS_ATOMIC- See Also:
-
-
Constructor Details
-
ComputeGraph
Create a ComputeGraph backed by the given GPU runtime.- Parameters:
runtime- the native driver abstraction (Metal, CUDA, etc.)
-
-
Method Details
-
ensureGpuBuffer
Ensure a tensor has a GpuBuffer. If it already has one (from a previous op), reuse it. Otherwise allocate a new buffer and schedule upload of its CPU data. -
newOutputBuffer
Allocate a new output buffer (result of a GPU op). Not yet allocated on native side. -
createOutputTensor
Create a Tensor backed by a GpuBuffer. The data[] buffer is allocated but stale. -
bindTensorToBuffer
Rebind an existing tensor to a GPU buffer and track ownership for lifecycle management. -
recordBinary
Record a binary element-wise op: [opCode, aId, bId, outId, n] -
recordMatmul
Record matmul: [OP_MATMUL, aId, bId, outId, m, n, k] -
recordUnary
Record a unary op: [opCode, inId, outId, n] -
recordMultiplyScalar
Record scalar multiply: [OP_MULTIPLY_SCALAR, inId, outId, scalarBits, n] -
recordScalarUnary
Record scalar add/divide: [opCode, inId, outId, scalarBits, n] -
recordPow
Record pow: [OP_POW, inId, outId, exponentBits, n] -
recordClamp
Record clamp: [OP_CLAMP, inId, outId, minBits, maxBits, n] -
recordScatterAddRows
public void recordScatterAddRows(GpuBuffer target, GpuBuffer indices, GpuBuffer grad, int targetRows, int targetCols, int nIndices) Record scatter-add-rows: [OP_SCATTER_ADD_ROWS, targetId, indicesId, gradId, targetRows, targetCols, nIdx] -
recordScatterAddRowsAtomic
public void recordScatterAddRowsAtomic(GpuBuffer target, GpuBuffer indices, GpuBuffer grad, int targetRows, int targetCols, int nIndices) Record scatter-add-rows (atomic): [OP_SCATTER_ADD_ROWS_ATOMIC, targetId, indicesId, gradId, targetRows, targetCols, nIdx] -
recordSumAbs
Record sum-abs row reduction: [OP_SUM_ABS, inId, outId, rows, cols] -
recordCrossEntropyLoss
public void recordCrossEntropyLoss(GpuBuffer logits, GpuBuffer targets, GpuBuffer out, int rows, int cols) Record cross-entropy row losses: [OP_CROSS_ENTROPY_LOSS, logitsId, targetsId, outId, rows, cols] -
recordCrossEntropyGradient
public void recordCrossEntropyGradient(GpuBuffer logits, GpuBuffer targets, GpuBuffer out, int rows, int cols) Record cross-entropy gradient: [OP_CROSS_ENTROPY_GRADIENT, logitsId, targetsId, outId, rows, cols] -
recordSumScalar
Record scalar sum reduction: [OP_SUM_SCALAR, inId, outId, rows, cols] -
recordTranspose
Record transpose: [OP_TRANSPOSE, inId, outId, rows, cols] -
recordRowBroadcast
public void recordRowBroadcast(int opCode, GpuBuffer a, GpuBuffer rowVec, GpuBuffer out, int rows, int cols) Record row broadcast: [opCode, aId, rowVecId, outId, rows, cols] -
recordColBroadcast
public void recordColBroadcast(int opCode, GpuBuffer a, GpuBuffer colVec, GpuBuffer out, int rows, int cols) Record col broadcast: [opCode, aId, colVecId, outId, rows, cols] -
recordReduction
Record row/col reduction: [opCode, inId, outId, rows, cols] -
recordSoftmaxRows
Record softmax rows: [OP_SOFTMAX_ROWS, inId, outId, rows, cols] -
recordSoftmaxBackward
public void recordSoftmaxBackward(GpuBuffer gradOutput, GpuBuffer softmaxOut, GpuBuffer out, int rows, int cols) Record softmax backward: [OP_SOFTMAX_BACKWARD, gradId, softmaxId, outId, rows, cols] -
recordLayerNormBackward
public void recordLayerNormBackward(GpuBuffer dXHat, GpuBuffer xHat, GpuBuffer std, GpuBuffer out, int rows, int cols) Record layer norm backward: [OP_LAYERNORM_BACKWARD, dXHatId, xHatId, stdId, outId, rows, cols] -
recordAdamWUpdate
public void recordAdamWUpdate(GpuBuffer w, GpuBuffer g, GpuBuffer mt, GpuBuffer vt, float lr, float beta1, float beta2, float eps, float weightDecay, float bc1, float bc2, int n) Record in-place AdamW update: [OP_ADAMW_UPDATE, wId, gId, mtId, vtId, lrBits, beta1Bits, beta2Bits, epsBits, weightDecayBits, bc1Bits, bc2Bits, n] -
isEmpty
public boolean isEmpty() -
flush
public void flush()Flush all recorded ops to the GPU as a single command buffer. After flush, GPU buffers hold computed results; CPU data is stale. -
materialize
Materialize a tensor: flush pending ops if needed, then download GPU data to CPU. -
releaseAll
public void releaseAll()Release all GPU buffers and reset the graph completely.
-