Device
Interface in Runtime
The owner of a host's kernel calls: the tier they run on, the Buffers they take and the pool those arrays return to, the device's limits and lost signal, the plans of fuse(), and dispose().
import type { Device } from 'typeshade'import type { Device } from 'typeshade/runtime'
The signature, the description and the examples come from the compiler's own source at commit af54ddad5.
Syntax
interface Device { readonly info: DeviceInfo; readonly tier: Tier; readonly fp64Flavor: Fp64Flavor; readonly skipped: readonly { readonly tier: Tier; readonly reason: string }[]; readonly limits: Readonly<Record<string, number>>; readonly lost: Promise<{ readonly reason: string; readonly message: string }>; array<T extends ScalarArray>(value: T): Buffer<ElementOfTyped<T>>; array(value: readonly number[]): Buffer<number>; array<E extends ZeroedElement>(length: number, element: E): Buffer<ZeroedBufferElement<E>>; texture(options: { readonly size: readonly [number, number]; readonly format: string; readonly storage?: boolean; }): Texture; release(array: AnyBuffer): void; readonly statistics: DeviceStatistics; fuse( body: (scope: FuseScope) => void, options?: { readonly chunk?: { readonly key: string; readonly units: number } }, ): Promise<FuseReport>; readonly timeBudgetMs: number; chunkSize(key: string, max: number, min?: number): number; readonly chunkRates: Readonly<Record<string, ChunkRate>>; wait(): Promise<void>; dispose(): Promise<void>; use(): DeviceUse;}Description
Made by createDevice. Two devices in one context are
independent: each has its tier, its arrays, its pool and its plans. Ordinary kernel, compute-entry and draw calls infer this owner from its arrays and textures. Mixing owners is refused before execution. A kernel or entry call without owned resources uses
the device in use (use(), change 0138, R3), else the default owner; a draw, the default owner.
Instance properties
inforead onlyDeviceInfoFrozen historical metadata of the opened device (change 0083), readable after loss or disposal. It does not identify the effective tier of each call.
tierread onlyTierThe tier that opened:
'webgpu','webgl2'or'cpu'.fp64Flavorread onlyFp64FlavorThe resolved flavor of the emulated double. Automatic selection is resolved when this device opens; this property is always
'float'or'integer'(change 0085).skippedread onlyreadonly { readonly tier: Tier; readonly reason: string }[]Each tier of
preferbefore the one that opened, with the reason it did not open.limitsread onlyReadonly<Record<string, number>>The device’s WebGPU limits, by their names in
GPUSupportedLimits, frozen (change 0051). Empty on a tier that has none.lostread onlyPromise<{ readonly reason: string; readonly message: string }>Resolves when the browser invalidates the WebGPU device. On another tier it never resolves.
statisticsread onlyDeviceStatisticsWhat the device counts of its arrays.
timeBudgetMsread onlynumberThe time one chunk of a long kernel may take, in milliseconds:
createDevice({ timeBudgetMs })’s value, 250 when none was given (change 0074).chunkRatesread onlyReadonly<Record<string, ChunkRate>>What the device measured of each chunk key a chunked
fuse()named, by key (change 0074). A frozen copy, empty before the first chunk.
Instance methods
array<T extends ScalarArray>(value: T): Buffer<ElementOfTyped<T>>A
Bufferof scalars that holds a copy of the typed arrayvalue, asarray(value)makes one (Rule 11.8; change 0138, Amendment A10, Q-I), owned by this device: taken from the pool when a released array of the same element and length is there, and made new otherwise.array(value: readonly number[]): Buffer<number>A
Buffer<f32>of the numbers ofvalue, asarray(value)makes one: anumber[]is held as aFloat32Array(change 0138; the owner’s decision of 2026-11-11).array<E extends ZeroedElement>(length: number, element: E): Buffer<ZeroedBufferElement<E>>A zeroed
Bufferoflengthnumbers ofelement, asarray(length, element)makes one (change 0065), owned by this device: taken from the pool and zeroed on the device when a released array of the same element and length is there, and made new otherwise.texture(options: { readonly size: readonly [number, number]; readonly format: string; readonly storage?: boolean; }): TextureA 2D texture of
sizeandformat, owned by this device (change 0077): a sampled texture, a copy source and destination and a render target, as a program runtime’s texture is, and a storage texture a kernel function writes whenstorageis true. A call takes it as a texture parameter, and a call of another owner refuses it.dispose()destroys it. On a device with no WebGPU device it is a texture of the CPU tier’s memory, of a format that tier holds.release(array: AnyBuffer): voidGive an array back. The device keeps it for a later
array()of the same element and length while the pool has room, and frees it otherwise. The handle is not used after.fuse(body: (scope: FuseScope) => void, options?: { readonly chunk?: { readonly key: string; readonly units: number } }): Promise<FuseReport>Record the kernel calls and entry calls
bodymakes and run them as one plan on this device (Rule 11.13). A call inside the body that takes aBufferof another owner is refused with aTypeErrornaming both owners. No registration import is needed (change 0085). The body runs synchronously; the queued plan automatically loads its local executor. Deploy every emitted chunk. A loading failure reaches each recorded call and written Buffer. A failed content dependency rejects the whole plan with the same error (0129). An earlier write-only call in the plan can recover a handle before a later call reads it.With
{ chunk: { key, units } }the plan is one chunk of a long kernel (change 0074):keynames the kernel loop andunitsis the work the chunk holds. The plan runs alone, at the placefusewas called. The device first waits for the work submitted before, aswait()does, and starts a wall clock (performance.now()). It runs the plan, fences it aswait()does and stops the clock. It learns the rate ofkey(chunkRates), and the report carries thechunkrow. The promise resolves after the fence. A plan with nochunkruns as before and does not wait for the fence.chunkSize(key: string, max: number, min?: number): numberThe units of the next chunk of
key(change 0074). With no chunk ofkeymeasured yet, it ismin, capped atmax: the first chunk teaches the rate. After that, it is the units that fit eight tenths oftimeBudgetMsat the learned rate, at most four times the last chunk’s units, at leastminand at least 1. It is never more thanmax. On the CPU tier, which has no watchdog, it ismax, so the whole work runs as one chunk.wait(): Promise<void>Resolves when the device has completed every plan submitted so far: on WebGPU
queue.onSubmittedWorkDone(), after the kernel queue has settled; on the other tiers when the calls have settled.dispose(): Promise<void>Wait for the plans in flight. Land what a call of this device wrote into each exported storage array, and drop this device’s copy of it (change 0138, R3). Then destroy every array the device holds, pooled or not, each once, and destroy the WebGPU device the owner requested. A
GPUDeviceor a runtime the host handed in is left as it is. A use afterdispose()is aTypeError.use(): DeviceUseMake this device the device in use until the returned handle is disposed (the default device of change 0138, Amendment A2):
using _ = device.use(). Uses nest, and the innermost is the device in use.texture(),zeros(),array()anduniform()make what they make on it, and a kernel call or an entry call with no owned resource runs on it (change 0138, R3). AUniformtakes no part in the choice of a call’s device. The uses are one stack for each instance of the runtime module, not one for each async task: while a task awaits inside a block, a resource or a call with no owned resource that another task makes takes this device. Caution: do not await inside a block while other tasks may make resources or calls. Where thelibdeclares noSymbol.dispose, calldispose()on the handle.
See also
Source
src/runtime/device.ts, line 147, at commit af54ddad5