Learn · 1
Tensors
A tensor is an array with a shape, living in GPU memory. Everything below runs in this page — edit any block and press Run, or ⌘/Ctrl + Enter.
Acquiring the device
await init() comes before anything else. Getting a WebGPU adapter is
asynchronous and there is no way around it — call it once, then forget it.
If it fails, borch stops there rather than falling back to something slower.
Making one
Tensor.from(data, shape) takes a flat array and the shape you want.
The factory names are torch's: zeros, ones,
arange, randn, eye, linspace.
Reading values is awaited
This is the second place borch differs from torch. The numbers live in GPU memory,
and getting them back to JavaScript is a copy — so item() and
toArray() return promises. Forward and backward passes are
synchronous; only reading crosses back.
Reductions that torch splits in two are split here too:
amax() gives the value, max(dim) gives
{ values, indices }.
Operations
Elementwise operations broadcast the way torch's do. Note that operators take
tensors, not numbers — JavaScript has no operator overloading, so a scalar is
written Tensor.full([], 5), and powScalar(k) exists for
the common case.
Shapes and views
reshape, transpose, slice and friends give
you a view onto the same storage where torch would — the conformance suite checks
that 13 of these share storage exactly as torch does, because a copy where torch
shares is a bug you only notice much later.