fuse
On this page

fuse()

Function in Runtime

Run a chain of kernel-function calls as one plan, and resolve to a report of what the plan did.

import { fuse } from 'typeshade/fuse'

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

Syntax

function fuse(body: (scope: FuseScope) => void): Promise<FuseReport>

Parameters

body (scope: FuseScope) => void

a synchronous function that makes the kernel calls to plan; it receives the scope, which counts the calls recorded so far and makes scratch arrays.

Return value

Promise<FuseReport>

the report of the plan once it has run.

Exceptions

TypeError

TypeError, as a rejection, for a body that is not a synchronous function. The errors of a call, such as a value that does not fit, are rejections too. Inside a body, a call that cannot be recorded throws.

Description

The runtime runs body once, synchronously. A kernel call the body makes whose written arrays are all scalar Buffers is recorded and not yet run. When body returns, the calls run as one unit at the place fuse was called, in the order of Rule 11.8: a call or a read() made after fuse returns sees what they wrote. On WebGPU the dispatches of all the calls are encoded into one command encoder and reach the queue in one submission. On WebGL2 each loop draws as it does outside a scope, and on the CPU tier the calls run one after another. Nothing is read back while the plan runs, since every array it writes is a Buffer. A reduction is in that submission too. Its loop and the dispatches that fold its partials share one device buffer of partials. The same submission copies the folded values, and the result of the call is made from them once the plan has run. A call that runs on WebGL2 or the CPU tier is the exception: it reads its arrays from the host, so the plan submits what it holds before it.

The plan fuses what the facts allow. Consecutive calls that run on WebGPU become one dispatch when each is the one loop of an eligible kernel carrying the default compiler facts, the loop neither scatters nor continues of its own accord, the loops have one start, one trip count and one step, an array one call wrote and a later call reads is written and read at exactly the loop’s index, no array a call writes is one an earlier call of the group reads or writes, and the group binds no more arrays than the device allows storage buffers, the partials of a reduction among them. A loop that reduces (s += xs[i]) may be the last call of a group: the calls before it run as a prologue of each of its iterations, and its folds of the partials, and the result of its call, are as they are outside a scope (Rule 7.2). No call joins a group after a call that reduces. A call that does not join the group before it starts a group of its own, and the report says why in the author’s words: “mul and blur run as two dispatches because blur reads t[i - 1] (line 9)”. A module built with fuse: false carries no facts, and its calls run apart. WebGL2 runs the calls apart, and the CPU tier never fuses. A call outside a scope is not changed.

Failed incoming Buffer contents reject the whole plan before execution (change 0129). An earlier approved write-only call can recover a handle for a later call in the same plan. Every recorded promise and planned output retains a rejection; independent calls outside continue.

scope.scratch(length) makes an intermediate that the host never reads. Where the plan fuses every call that takes it, and each reaches it at exactly the loop’s counter and never by its length, the fused entry holds it as a local, and no device buffer and no host array is made for it. Otherwise it is a zeroed array that exists for the plan and is freed when the plan has run. read() and set() of a scratch array, and a call that takes it outside the scope that made it, are a TypeError. A Buffer from array() keeps its buffer and its write.

A kernel call or an entry call inside the body is recorded and runs in the plan, in the order the body made it. An entry call forms a group of its own, so no kernel call joins it; its storage arrays with no size must be Buffers, and it writes no host value. A call inside the body that cannot be recorded is a TypeError naming the call: a draw, a kernel call that writes a host array, which would have to be read back, and an entry call that takes a host array or writes a host value. So are read(), set() and dispose() of a Buffer and every other use of the kernel queue, since they would run before the calls the body recorded earlier. Close the scope around it. A fuse call inside the body joins the scope, and returns that scope’s report. The body is synchronous: an async function is a TypeError, since a call after its first await would run outside the scope. A body that throws leaves nothing to run.

The promise rejects with the first error of the plan. The plan is one unit: the error also stays on every Buffer any of its calls writes, for read() to throw, as it does outside a scope. Every call is checked before any runs, so a call that fails a check fails the plan before any call of it ran.

Examples

Example

import { array } from 'typeshade/runtime';
import { fuse } from 'typeshade/fuse';
import { add, mul, relu } from './ops.shade.ts';
const out = array(new Float32Array(n));
const report = await fuse((scope) => {
const t = scope.scratch(n);
const u = scope.scratch(n);
add(t, a, b);
mul(u, t, c);
relu(out, u);
});
report.dispatches; // 1, on WebGPU: t and u are locals of the one entry
const y = await out.read();

In the guide

Source

src/fuse.ts, line 104, at commit af54ddad5

Edit this page Report a problem