Skip to content

GPU Programs

An agent that uses caliper-mcp can write a WGSL program that runs on the GPU over a live tensor of a training script. The program draws new geometry in a scene panel or new pixels in an image panel. The script runs a program in its own process, on the tensor in place, and the program never writes the tensor. A program runs only in a build of Caliper that has the WGSL compiler library, which a build from source gets with CALIPER_WGSL_FROM_SOURCE=ON. Run Control and Events describes the intervention tools and the toggle control that came with this work.

Tools

Each command is one MCP tool with the first dot of its name replaced by _, such as program_create. The first three commands edit the open spec and give a diff, one undo step and a journal entry, as the panel commands do.

Command Arguments Function
program.create The keys of a program declaration, dry_run Adds a program to the open spec
program.patch id, a merge patch, dry_run Changes a program and checks it again
program.remove id Removes a program
program.status id Returns the state of each program

The app sends each program of the open spec to the script that it is attached to. It sends them again after each new attachment. Without an attached script, the state of a program is waiting. The person's own agent needs no review card for a program, because the sandbox is always on and the panel shows the source. The review settings of the other spec edits apply to these edits.

Programs

A program has one compute pass with one dispatch. Its declaration holds the WGSL source, its inputs, one output and its limits.

Key Meaning
id The id of the program, at most 55 characters
wgsl The WGSL source, at most 64 KiB
entry The compute entry point. It is the one compute entry point when absent, and it is never named main.
inputs At most 8 pairs of var and source, where source is a live tensor live:<name>
output One output of kind geometry or pixels
limits time_ms, memory_mb and every, each optional

An input names a tensor of the script. The name is a parameter name of model.named_parameters(), such as 0.weight. A parameter name with .grad added is its gradient, and a module name with .act added is the last output of the module. The program declares each input as var<storage, read>. A gradient or an output that does not exist yet keeps the program in the state waiting.

A geometry output draws as one entity of a scene panel. It has the keys panel, entity (programs/<id> when absent), part (points or lines), max_count (at most 1,000,000), arrays and an optional style with colormap, vmin and vmax. The program declares each array of arrays as var<storage, read_write>, and also a counter count: atomic<u32>. The runtime sets the counter to zero before each pass, and the first count items of each array are valid after it. The array positions is necessary, with three floats for each item. The arrays colors (one packed RGBA8 value for each item), values (one float for each item) and radii (points only) are optional.

A pixels output has the keys width and height (each at most 2048) and format (rgba8unorm). The program declares pixels: array<u32> with one packed RGBA8 value for each pixel in row order. The script publishes the image as the live view program.<id>, and an image panel shows it through a source whose catalog id is live:program.<id>. Because of this name, a program id has at most 55 characters.

A program can also declare one uniform buffer with the name caliper. The runtime fills it before each pass with the step, the count of earlier passes, the sizes of the output, the seconds since the install, and the cadence of the pass.

Limit Default Maximum
time_ms, the GPU time of one pass 4 50
memory_mb, the output buffers and the bound tensors 64 512
every, the fewest steps between two passes 1 10000

Sandbox Rules

The sandbox is always on. The same checks run in the editor before an edit, and in the script before a program runs. A failed program is an error of its panel and never of the training script or the app. The checks run in this order, and the first failure stops the program with its code:

  1. The ids, the counts, the sizes and the limits are inside their maximums.
  2. The source is inside its size limit and has no enable, requires or diagnostic directive.
  3. The compiler parses and validates the source. It refuses a store to a var<storage, read> binding.
  4. A program has its compute entry point, with at most 256 invocations in a workgroup.
  5. Each binding is declared, each input is a read-only storage buffer, and each output has its kind and its access.
  6. The script imports each input in place on its device and never copies a training tensor. The import is read-only.
  7. The output buffers and the bound tensors are inside memory_mb.
  8. The frame validation of the graphics layer checks the bind groups and the dispatch.

A program has no file access, no network access and no access to a tensor that it does not declare. The time watchdog measures the GPU time of each pass. A program over time_ms on three passes in a row stops with program.over_time, and training goes on. A wait of a pass that lasts longer than ten times the sum of the limits of the due programs, and at least 100 ms, stops every program with program.timeout.

Errors

Each error has a code and one plain sentence. A message that comes from the compiler also has a line and a column in the source that the agent sent.

Code Cause
program.exists, program.not_found The id is already there, or is not there
program.bad_target The panel is not a scene panel, or the spec has no such panel
program.too_large, program.over_limits A size, a count or a limit is above its maximum
program.directive_not_allowed The source has a directive
program.compile_failed The compiler refused the source, or the entry point is named main
program.no_entry The source has no compute entry point, or not the named one
program.binding_undeclared, program.output_mismatch A binding is not declared, or an output has the wrong kind or access
program.writes_input An input is not var<storage, read>
program.compiler_unavailable The build has no WGSL compiler library

Without the compiler library, program.create applies the edit with the warning program.not_checked, and no program runs. A host reports these codes in the status item and in a program event: program.unknown_input, program.import_refused, program.over_memory, program.layer_refused, program.over_time, program.over_budget, program.timeout, program.device_lost and program.unavailable.

Program Status

program.status returns programs, and each item has the same keys. The key host is script for a program, viewer for the draw half of a piece, and none for an item that no host runs.

Key Value
id, kind, host, state The id, the kind (geometry, pixels, tensor, operator or piece), the host and the state (waiting, review, compiled, running, refused or stopped)
every, every_min The steps between two passes of the program, which is the shared cadence N or a multiple of N for a larger declared every, and the every of the declaration
last_pass_ms, last_cost_ms, step_ms The GPU time of the last pass, the pass cost D (in training runs, the overhead added to the wall clock; in interpretability runs, the program work time), and the base training step time b (null in interpretability runs)
overhead_share, passes_per_s The measured share D / (N * b) of training time (null in interpretability runs or before measurement), and the passes a second over the last 8 passes
last_step, runs The step of the last pass, and the count of passes
memory_bytes, memory_limit_bytes The memory in use and its budget
error, warning, sha256 The last error and any active warning as code, message, line and column, and the SHA-256 of the source that the host runs

The state review belongs to a spec from someone else. The object provenance of the spec marks such a spec, and a program in it runs only when its SHA-256 is in the list reviewed. No command writes provenance. The review card that adds a hash is not built yet.

Program Events

The tool events_wait takes the kind program. The keys of the event are program (the id of the status item), change, host, code, message, line and column, and the common key panel holds the target panel. The change compiled means that a host compiled the program. The change warned means that a program went over a budget such as program.over_budget or program.draw_over_time while it keeps running. The change refused means that a check refused it, and the change stopped means that a timeout, the memory budget, limits.time_ms or a lost device stopped it. An agent waits for the event instead of a fixed time.

Cadence

A program in a training run does not run on each step. It runs every N steps, and the script sets N by feedback from the time that the passes add to the wall clock. The aim is that the passes take at most 4 % of the training time. The rest of a 5 % loss stays for the hooks of caliper.watch(model, optimizer) and the publish path.

While a program is installed, the script records a torch.mps.Event at each step. The record gives the queued work of torch to the GPU without a wait. The script reads with query() which marks completed, and it takes the completion times from these reads. A step without a pass never waits.

From the completion times, it measures two times for each cycle between two passes. The base step time b is the median of the last 9 cycles. The overhead D is the mean of the last 8 cycles without the largest and the smallest. It is the time that a pass adds to the wall clock. Control pauses, actions and interpretation requests inside caliper.step() reset the open cycle so they do not count as program overhead.

Then N follows from this rule:

N_want = ceil(D / (0.04 * b))
N_max  = max(3, min(10000, ceil(10 s / b)))
target = min(max(N_want, 3), N_max)

N moves up by at most a factor of 2 for each update. It moves down by at most a factor of 1.5, and only when it is more than 25 % above the target. N is at least 3, and it is 3 before the first measured pass. The first pass of a program runs 8 steps after its install.

All programs share the pass steps, and one pass runs every due program. A declared every asks for a slower cadence and never for a faster one. When N_want stays above N_max for 3 updates in a row, and the smallest declared every is below N_want too, programs other than built-in library pieces get a recoverable program.over_budget warning and keep running. Built-in library pieces get no hard stop and no program.over_budget warning. A real GPU hang still stops every program with program.timeout. A loop that already waits for the GPU at each step, for example with loss.item(), gets a mark only every 8 steps, before a pass and at the pass.

The training-overhead cadence and program.over_budget do not apply to interpretability runs, where a step is not a training step. In an interpretability run, step_ms and overhead_share stay null, last_cost_ms measures only program work, and passes run without training-budget throttling.

program.status shows N as every, D as last_cost_ms, b as step_ms, the share of the training time as overhead_share and the rate of passes as passes_per_s.

With one geometry program under caliper.watch(model, optimizer), a small model with steps of about 0.4 ms kept 95.9 % of its speed in a quiet run. A medium model with steps of about 11 ms kept 96.8 %. Loops that read the loss at each step kept 97.4 % and 98.7 %.

On the small model, the marks themselves cost about 1 % to 3 % of its short steps, thus its loss is larger than the measured share. On a loaded machine, the small model kept 93.6 % to 94.4 %. Each number is the median of five runs on one Apple machine with torch on MPS. Thus a model with steps below 1 ms can keep less than 95 % of its speed when the machine is busy.

Views in the Cadence

The cadence of the programs also gates what viewers ask of the run. While the script calls caliper.step() and a viewer subscribes, snapshots, series chunks and scene colors run only on the pass steps. As a result, an attached instrument updates its panels a few times a second on a fast run. In the measurement, the panels updated 2.3 times a second when each step slept 10 ms, and 4.4 times a second at full speed.

Every value of a watched series, for example the loss, still arrives. The script gathers the value of each step on the device and moves the values in one chunk at each pass.

While views are attached, the passes keep to 2 % of the training time. GPU programs alone keep to 4 %. The script measures only its own part of a pass, and the work of the viewer on the same snapshots costs the run about as much again.

When the passes come less often than every 0.5 s, for example beside a costly program, the views can get a pass of their own every 0.5 s. They get it only while their own time in a pass is at most 2 % of 0.5 s, which is 10 ms. The cadence does not count such a pass, and the programs keep to N. A script that calls the views by hand without caliper.step() keeps its ticks on the clock, at most 60 a second. A script whose checkpoints stop for a second does the same.

Each snapshot of a pass carries the cadence in the SNAPSHOT key cadence for a peer that announced programs. The keys are every, passes_per_s, step_ms, pass_ms, share, budget and limit. See Wire Protocol 3.2. The live catalog of the app gives a live view that the cadence thinned a warning when limit is budget or floor. The tool catalog_list shows it in the warnings of the entry. For example:

Updates every 19 steps, about 3.4 times a second, because the views and programs that viewers attach keep to 2 % of the training time, and one pass of them costs 8.13 ms against a step of 22.12 ms.

A small model with a full instrument attached through the app, with live panels, kept steps, controls and an operator, kept 95.8 % of its speed on a quiet machine. A medium model under the same instrument kept 99.6 %. Before the budget, the same instrument left the small model 55 % of its speed. Each number is the median of five runs on one Apple machine with torch on MPS.