Compiling GPU Graphs
You can describe a GPU Graph before you have a device. Once you have a WebGPU device, choose the outputs you need and compile the graph for repeated execution.
Compile once and run with new inputs​
For the graph in the overview, result is the selected output and values is a caller-supplied GPU vector.
const compiledGraph = graph.compile(device, {outputs: ['result']});
try {
for (const values of batches) {
const {result} = await compiledGraph.run({inputs: {values}});
try {
// Use the result for this batch.
} finally {
result.destroy();
}
}
} finally {
compiledGraph.destroy();
}
The graph manages intermediate storage. Each run accepts new bindings and returns the selected outputs. See inputs, results, and state for output targets and ownership.
From operations to WebGPU commands​
A single operation, such as sparse matrix-vector multiplication, can require several dispatches. The WebGPU compiler translates the operations into a GPUCommandGraph containing compute, render, and copy passes. This translation is called lowering.
Use GPUGraphCompiler directly when you need to supply concrete compiler bindings or inspect the generated commands. The compilation result exposes the command graph and a report linking commands to the operations that produced them.
const compilation = new GPUGraphCompiler(device).compile(graph, bindings);
const commandGraph = compilation.graph;
const decisions = compilation.lowering.decisions;
Explicit compiler declarations​
Use the compiler declaration methods graph.compiler.scalar(name, format), graph.compiler.vector(name, format, ...),
and graph.compiler.texture(name, props) when constructing a program with explicit compiler
bindings. Each method adds a named declaration to this graph and returns a reference
for operations. graph.compiler.textureView(texture, props) selects a subresource of a declared
texture. The conjugate-gradient program uses explicit declarations for its solver vectors
and iteration scalars. A sparse matrix-vector product uses a GPUGraphCSRMatrix to group the row offsets, column indices, and values consumed by gpuSparseMatVec.
For reusable application graphs, declare external inputs with addInput and mutable
vectors with addState. An operation's contract declares its results. See
Graph inputs, results, and state.
Vector bindings and chunk topology​
A GPUData describes one physical chunk. A GPUVector is an ordered logical sequence of chunks.
GPUGraphVector declares its logical format and length, plus optional chunkLengths when the
application needs a particular partition. A single chunk follows the same compilation path.
const input = graph.compiler.vector('input', 'float32', batch0.length + batch1.length, {external: true});
const output = graph.compiler.vector('output', 'float32', input.length, {
chunkLengths: [batch0.length, batch1.length]
});
const compilation = new GPUGraphCompiler(device).compile(graph, {
vectors: {input: [batch0, batch1]} // Existing GPUData chunks; no upload or concatenation.
});
const chunks = compilation.vectors.get(input.id).chunks;
// Each descriptor has offset (logical rows), length, and data (a physical GPUGraphDataView).
Bindings accept a GPUVectorLike (including GPUVector), a single GPUData, or a readonly
GPUData[]. Raw Buffer bindings
must be wrapped in GPUData with explicit format and layout. Import preserves physical buffers,
byte offsets, row strides, empty chunks, and source order. Shared physical buffers share graph
hazard tracking. Imports borrow storage; destroying a compiled program does not destroy the source.
Both GPUVector.chunks and GPUGraphVectorView.chunks use the same metadata definition. A descriptor's
data holds canonical physical format, byte offset, byte stride and row payload size. Compiler
bindings snapshot the current source chunks; later appends require recompilation to participate.
resolveVector() and compilation.vectors always return GPUGraphVectorView. Explicit transient
chunkLengths are retained; otherwise the compiler partitions transients to fit the device's
storage-buffer binding capacity. External bindings must match logical format and length, and must
match explicit chunk lengths when supplied. They are never repacked to satisfy a declaration.
MADD intersects source and destination chunk boundaries through borrowed views. Dot product sums chunk partials into one scalar, resetting the result on every invocation. These lowerings retain per-node dispatch geometry for GPU predicates. Neither requires matching source batches or a contiguous copy. Current numeric kernels require packed float32 rows; strided layout can be bound and inspected but is rejected by kernels that do not support it.
CSR SpMV resolves all operands as graph vectors. Row offsets, nonzero columns/values, the dense input, and output may have independent chunk boundaries. Cross-chunk rows use global indices; source buffers remain borrowed, long-row scratch is bounded, and GPU predicates retain each node's exact dispatch geometry. See the individual operation contracts for specialized domain constraints and performance costs.
Shared vector shape​
GPUVectorLike<Format, Data> is the structural, read-only contract for an ordered vector. Its
required fields are length and data; format can be inferred from nonempty chunks. Optional
aggregate metadata is inferred during graph import. GPUVector and GPUGraphVectorView implement
this contract using their respective physical and graph-managed chunk types. It carries no
allocation or destruction methods, so other implementations do not need to subclass GPUVector.
WebGPU graph bindings accept structural vectors whose chunks are GPUData, without creating
an intermediate GPUVector wrapper:
compiler.compile(graph, {
vectors: {input: {format: 'float32', length: 5, data: inputChunks}}
});
GPUVectorInput names the shared storage input union in gpu-data; there is no program-specific
vector-binding type. GPUGraphBindings is only a TypeScript object type for the named bindings,
not a runtime wrapper. GPUGraphVector remains a symbolic declaration: it can describe transient
storage before a device or buffer exists. Keeping this declaration separate avoids adding program
identity, external-binding flags, or compiler allocation state to ordinary GPUVector instances.
Buffer and texture management​
See Buffers and textures for logical values, views, imported and transient ownership, hazard tracking, history, resizing, and encoding contracts.
graph.compiler.texture(id, props) declares logical texture storage using the graph texture descriptor
fields plus external. graph.compiler.textureView(texture, props) selects subresources without allocating
storage. GPUGraphCompiler.compile() accepts optional textures defaults; imported textures may
also be supplied when encoding. compilation.textures exposes logical graph texture handles.
Custom lowerers resolve storage with resolveTexture() and views with resolveTextureView().
CompiledGPUGraph.createState() creates a GPUGraphState with explicit borrowing or copying,
reset, and destruction. See Mutable graph state.
Reusable compile(device) programs also support named texture inputs and operation-produced
texture results. See texture values and execution bindings.
Adapting command node producers​
gpuCommand gives a command-node producer a logical value contract and named ports.
The contract declares reads, writes, output types, and shapes for planning. The factory receives
resources resolved for each execution, including the chunk boundaries produced by preceding CPU
operations.
import {gpuCommand} from '@luma.gl/graph/gpu-graph';
import {GPUScan} from '@luma.gl/graph/gpu-primitives';
program.add(gpuCommand, {
id: 'prefixes',
ports: {input: 'values', output: 'prefixes'},
contract: {
outputs: {prefixes: {type: 'vector', format: 'uint32'}},
values: [
{value: 'values', access: 'read'},
{value: 'prefixes', access: 'write', shape: {like: 'values'}}
]
},
createNodes: ({resolveVector}) => new GPUScan({
input: resolveVector<'uint32'>('input'),
output: resolveVector<'uint32'>('output')
})
});
Factories can return a producer, concrete nodes, or nested groups accepted by GPUCommandGraph.add.
Port names are local to the operation. Every port maps to a value declared in the contract.
Use resolveScalar, resolveVector, resolveTexture, or resolveTextureView with a port name.
The resolved format must match the producer's input contract. The normal command graph checks
resource ownership and hazards, and preserves vector chunk boundaries.