| ag_graph_mode | R Documentation |
With graph mode on, a GPU ag_* operation that has a device-resident
path does not run at once: it adds its node to a queue and returns a pending
value. The queue runs as ONE ggml graph when a value is read on the host
(as.matrix(), a printed loss, an action index) or when
backward() needs it – the graph backward then joins the same graph.
Each separate launch costs on the order of a millisecond on the device, so a
small network (an RL policy, a few layers) is dominated by launches, and
fusing them is where the speed comes from.
ag_graph_mode(on, check = NULL)
on |
|
check |
|
Results are the same as without it. Operations that have no resident path yet still run one by one; they first run whatever is queued, so the order of computation is always the data order.
When the queue runs (synchronisation points):
reading a value that is still queued: as.matrix(), printing,
a loss value, ag_argmax();
an operation without a resident path (it runs the queue first);
writing a parameter: an optimizer step, ag_load_state_dict();
the device optimizer step (it never joins the queue);
turning graph mode off.
backward() does NOT force it: the graph backward appends to the
queue, so forward and backward run as one graph at the next point above.
Off by default; turn it on around the code that wants it, restoring the
previous value on every exit – ag_local_mode does that. The one
thing it can break is code that orders graphs BY HAND rather than through
data – reading a buffer in one graph and overwriting it in a later one with
nothing linking the two. The package's own optimizer step does this and is
protected; code of your own that does it must run with graph mode off.
On the CPU it has no effect.
Errors. A failure inside a queued operation surfaces where the queue
runs, not where the operation was called. The error names the queued
operations; check = TRUE (or GGMLR_AG_GRAPH_CHECK=1) runs each
operation as soon as it is queued, so the error appears at its own call –
for debugging, since it gives up the fusion.
The current setting when on is missing; otherwise the previous
setting, invisibly. Turning it off runs anything still queued.
ag_local_mode, ag_matmul_precision
old <- ag_graph_mode(TRUE)
ag_graph_mode()
ag_graph_mode(old)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.