@margoengine/compute 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +804 -2
- package/dist/array-C29noZq7.js +166 -0
- package/dist/array.d.ts +109 -0
- package/dist/autodiff.d.ts +22 -0
- package/dist/autodiff.js +143 -0
- package/dist/autotune.d.ts +100 -0
- package/dist/autotune.js +179 -0
- package/dist/compat-YK57ZCbA.js +4 -0
- package/dist/compat.d.ts +2 -0
- package/dist/context-CCvs3wkC.js +2479 -0
- package/dist/context.d.ts +376 -0
- package/dist/cpu-D4UQwJ9L.js +871 -0
- package/dist/cpu-kernel.wasm +0 -0
- package/dist/cpu-load-XFcM1TXj.js +5 -0
- package/dist/cpu-load.d.ts +3 -0
- package/dist/cpu-runtime.d.ts +8 -0
- package/dist/cpu-wasm.d.ts +42 -0
- package/dist/cpu-wasm.js +161 -0
- package/dist/cpu.d.ts +138 -0
- package/dist/decomp-kernel.wasm +0 -0
- package/dist/decomp.d.ts +42 -0
- package/dist/decomp.js +158 -0
- package/dist/distance.d.ts +85 -0
- package/dist/distance.js +256 -0
- package/dist/dtype.d.ts +36 -0
- package/dist/errors-CDgOgzCd.js +7 -0
- package/dist/expr-t3jm8acM.js +996 -0
- package/dist/expr.d.ts +196 -0
- package/dist/fft.d.ts +33 -0
- package/dist/fft.js +168 -0
- package/dist/frame.d.ts +76 -0
- package/dist/frame.js +85 -0
- package/dist/graph.d.ts +75 -0
- package/dist/graph.js +174 -0
- package/dist/histogram.d.ts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +4 -0
- package/dist/kernel.d.ts +65 -0
- package/dist/linalg.d.ts +21 -0
- package/dist/linalg.js +149 -0
- package/dist/nn-extra.d.ts +130 -0
- package/dist/nn-extra.js +322 -0
- package/dist/nn.d.ts +385 -0
- package/dist/nn.js +704 -0
- package/dist/onnx-image-D2qfYsXf.js +66 -0
- package/dist/onnx-image.d.ts +10 -0
- package/dist/onnx-kernel.wasm +0 -0
- package/dist/onnx-regex-DaesIdrm.js +331 -0
- package/dist/onnx-regex.d.ts +2 -0
- package/dist/onnx.d.ts +167 -0
- package/dist/onnx.js +885 -0
- package/dist/quant.d.ts +59 -0
- package/dist/quant.js +242 -0
- package/dist/random.d.ts +64 -0
- package/dist/random.js +150 -0
- package/dist/reduce.d.ts +34 -0
- package/dist/scan.d.ts +26 -0
- package/dist/select.d.ts +38 -0
- package/dist/shaders.d.ts +80 -0
- package/dist/shape.d.ts +23 -0
- package/dist/shape.js +134 -0
- package/dist/signal.d.ts +29 -0
- package/dist/signal.js +178 -0
- package/dist/sort.d.ts +38 -0
- package/dist/sparse.d.ts +136 -0
- package/dist/sparse.js +273 -0
- package/dist/stream.d.ts +39 -0
- package/dist/stream.js +100 -0
- package/dist/subgroups.d.ts +27 -0
- package/dist/subgroups.js +191 -0
- package/dist/usingCtx-igdMnAhr.js +57 -0
- package/dist/wgsl.d.ts +68 -0
- package/package.json +146 -4
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { DType } from './dtype.js';
|
|
2
|
+
import type { Fused } from './expr.js';
|
|
3
|
+
/** Threads per workgroup for every primitive. */
|
|
4
|
+
export declare const WG = 256;
|
|
5
|
+
/** Elements each reduce or scan workgroup covers (four per thread). */
|
|
6
|
+
export declare const BLOCK = 1024;
|
|
7
|
+
/** @internal Kernels an optional entry puts in place of the default ones, with the same bindings. */
|
|
8
|
+
export interface KernelRoute {
|
|
9
|
+
/** A tree level of a full reduction, or undefined to keep the default kernel. */
|
|
10
|
+
level(src: Fused, combine: string, identity: string): string | undefined;
|
|
11
|
+
/** One workgroup per output along an axis, or undefined. */
|
|
12
|
+
axis(src: Fused, combine: string, identity: string): string | undefined;
|
|
13
|
+
/** One level of a flat scan, or undefined. */
|
|
14
|
+
scan(src: Fused, combine: string, identity: string): string | undefined;
|
|
15
|
+
/** The radix sort's local pass. */
|
|
16
|
+
readonly radixLocal: string;
|
|
17
|
+
}
|
|
18
|
+
/** A binary operator: a built-in name or WGSL for `combine(a, b)` with its identity value. */
|
|
19
|
+
export type Operator = 'sum' | 'product' | 'min' | 'max' | {
|
|
20
|
+
readonly combine: string;
|
|
21
|
+
readonly identity: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* WGSL for `combine(a, b)` and the identity of `op` on `type`. Float infinities have no WGSL literal, so float min/max
|
|
25
|
+
* identities read `params.inf` (0x7f800000, the f32 +Infinity bits; the f16 bits are derived from it).
|
|
26
|
+
*/
|
|
27
|
+
export declare function operator(op: Operator, type: DType): {
|
|
28
|
+
combine: string;
|
|
29
|
+
identity: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* One scan level: each workgroup scans 1,024 values into `dst` (inclusive, or exclusive when `params.a` is 1) and
|
|
33
|
+
* writes its total to `sums[group]`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function scanShader(src: Fused, combine: string, identity: string, threads?: number): string;
|
|
36
|
+
/** Puts each block's scanned prefix `sums[group]` in front of the block's 1,024 elements of `data`. */
|
|
37
|
+
export declare function addShader(type: DType, combine: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* One inclusive segmented scan level over (flag, value) pairs. Flags come from `flagsIn` or, when `periodic`, start a
|
|
40
|
+
* segment every `params.b` elements (a scan along the last axis). Writes the block-local result to `dst`, whether a
|
|
41
|
+
* segment started in the block at or before each element to `seen`, and each block's (any flag, last value) total.
|
|
42
|
+
*/
|
|
43
|
+
export declare function segmentedShader(src: Fused, combine: string, identity: string, periodic: boolean): string;
|
|
44
|
+
/** Adds the running value of the previous block (`scanned[group - 1]`) to elements before their block's first flag. */
|
|
45
|
+
export declare function segmentedAddShader(type: DType, combine: string): string;
|
|
46
|
+
/** An input of a map: its name in the expression and its element type. */
|
|
47
|
+
interface MapInput {
|
|
48
|
+
readonly name: string;
|
|
49
|
+
readonly type: DType;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Element-wise map: `out[i] = expression` with each input's element bound to its name, `i` the index and `n` the
|
|
53
|
+
* size. An expression containing `return` is used as the function body instead.
|
|
54
|
+
*/
|
|
55
|
+
export declare function mapShader(inputs: readonly MapInput[], output: DType, expression: string): string;
|
|
56
|
+
/**
|
|
57
|
+
* Sort keys from the input: floats flip all bits of negatives and the sign bit of positives, signed integers flip the
|
|
58
|
+
* sign bit, so unsigned order is value order; `params.a` 1 inverts them for a descending sort. Also writes 0, 1, 2,
|
|
59
|
+
* ... into `order` when `params.b` is 1 (the payload of an argsort).
|
|
60
|
+
*/
|
|
61
|
+
export declare function keyShader(src: Fused): string;
|
|
62
|
+
/** Turns sorted u32 keys back into values of `type` (undoing `keyShader`). */
|
|
63
|
+
export declare function fromKeyShader(type: DType): string;
|
|
64
|
+
/**
|
|
65
|
+
* Radix pass, step 1: each block of `threads` keys sorts itself on the 4-bit digit at `shift` (four stable one-bit splits),
|
|
66
|
+
* writes the sorted block, its digit counts (`counts[digit * blocks + block]`) and digit starts
|
|
67
|
+
* (`starts[block * 16 + digit]`). Slots past `n` hold 0xffffffff keys, which sort after every real key of the block.
|
|
68
|
+
*/
|
|
69
|
+
export declare const radixLocal: (threads: number) => string;
|
|
70
|
+
/** Radix pass, step 2 (after the counts are scanned into offsets): moves each key of a sorted block to its place. */
|
|
71
|
+
export declare const radixScatter: (threads: number) => string;
|
|
72
|
+
/** Most bins counted in workgroup memory (16 KB); more bins count straight into the global array. */
|
|
73
|
+
export declare const LOCAL_BINS = 4096;
|
|
74
|
+
/**
|
|
75
|
+
* Histogram: counts values in `bins` equal bins over [lo, hi] (read from `range`, which the GPU may have just
|
|
76
|
+
* computed) into `counts`, the last bin closed, values outside the range and NaN skipped. `local` counts in workgroup
|
|
77
|
+
* memory first.
|
|
78
|
+
*/
|
|
79
|
+
export declare function histogramShader(src: Fused, local: boolean): string;
|
|
80
|
+
export {};
|
package/dist/shape.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type GpuArray } from './array.js';
|
|
2
|
+
import { Operand } from './expr.js';
|
|
3
|
+
/** Join along an existing axis (default 0); null flattens all inputs. Types promote as for arithmetic. */
|
|
4
|
+
export declare function concatenate(arrays: readonly Operand[], axis?: number | null): GpuArray;
|
|
5
|
+
/** Join equal shapes along a newly inserted axis (default 0, negative axes allowed). */
|
|
6
|
+
export declare function stack(arrays: readonly Operand[], axis?: number): GpuArray;
|
|
7
|
+
/** Reverse the given axes, or every axis when omitted. Returns an independently owned array. */
|
|
8
|
+
export declare function flip(x: Operand, axis?: number | readonly number[]): GpuArray;
|
|
9
|
+
/** Tile whole arrays, prepending singleton axes/repetitions to match ranks, as in NumPy. */
|
|
10
|
+
export declare function tile(x: Operand, repetitions: number | readonly number[]): GpuArray;
|
|
11
|
+
/** Repeat individual elements/slices. Omitted/null axis flattens. A count list broadcasts from length 1. */
|
|
12
|
+
export declare function repeat(x: Operand, repeats: number | readonly number[], axis?: number | null): GpuArray;
|
|
13
|
+
/** Padding widths: one size, a shared [before, after], or one pair per dimension. */
|
|
14
|
+
export type PadWidth = number | readonly [number, number] | readonly (readonly [number, number])[];
|
|
15
|
+
/** Constant, endpoint, periodic, or mirrored padding. Mirrors can extend beyond the input length. */
|
|
16
|
+
export interface ShapePadOptions {
|
|
17
|
+
/** Default 'constant'. */
|
|
18
|
+
readonly mode?: 'constant' | 'edge' | 'wrap' | 'reflect' | 'symmetric';
|
|
19
|
+
/** Scalar fill for constant padding. Default 0, converted to the input dtype. */
|
|
20
|
+
readonly constantValues?: number;
|
|
21
|
+
}
|
|
22
|
+
/** Pad each axis, preserving dtype. Nonconstant modes require nonempty source axes when output is nonempty. */
|
|
23
|
+
export declare function pad(x: Operand, width: PadWidth, options?: ShapePadOptions): GpuArray;
|
package/dist/shape.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { n as e, r as t } from "./errors-CDgOgzCd.js";
|
|
2
|
+
import { m as n, n as r, t as i } from "./expr-t3jm8acM.js";
|
|
3
|
+
import { n as a } from "./array-C29noZq7.js";
|
|
4
|
+
//#region src/shape.ts
|
|
5
|
+
var o = (e) => e.reduce((e, t) => e * t, 1), s = (e, t) => new i(e.context, {
|
|
6
|
+
kind: "index",
|
|
7
|
+
dtype: "u32",
|
|
8
|
+
shape: t
|
|
9
|
+
}).astype("i32"), c = (e) => Number.isSafeInteger(e) && e >= 0;
|
|
10
|
+
function l(e) {
|
|
11
|
+
if (!(e instanceof r)) throw t("Shape operations need a compute array or expression", "compute");
|
|
12
|
+
e.context.check();
|
|
13
|
+
let n = (t) => {
|
|
14
|
+
t.kind === "leaf" ? t.array.check(e.context) : t.kind === "op" && t.args.forEach(n);
|
|
15
|
+
};
|
|
16
|
+
n(e.node());
|
|
17
|
+
}
|
|
18
|
+
function u(r, i, c) {
|
|
19
|
+
if (!Array.isArray(r) || !r.length) throw t("concatenate/stack needs at least one array", "compute");
|
|
20
|
+
r.forEach(l);
|
|
21
|
+
let u = r[0], d = u.context, f = u.ndim + +!!c;
|
|
22
|
+
if (i !== null && !f) throw e("Use axis: null to concatenate scalars", "compute");
|
|
23
|
+
let p = i === null ? 0 : a(i, f), m = r.map((e) => {
|
|
24
|
+
if (e.context !== d) throw t("Shape operands must share a compute context", "compute");
|
|
25
|
+
let n = i === null ? [e.size] : [...e.shape];
|
|
26
|
+
return c && n.splice(p, 0, 1), n;
|
|
27
|
+
}), h = [...m[0]];
|
|
28
|
+
for (let t of m) if (t.length !== h.length || t.some((e, t) => t !== p && e !== h[t])) throw e("concatenate needs matching dimensions outside its axis; stack needs identical shapes", "compute");
|
|
29
|
+
h[p] = m.reduce((e, t) => e + t[p], 0);
|
|
30
|
+
let g = r.reduce((e, t) => n(e, t.dtype), u.dtype), _ = d.empty(h, g), v = o(h.slice(p + 1)), y = 0;
|
|
31
|
+
try {
|
|
32
|
+
for (let e = 0; e < r.length; e++) {
|
|
33
|
+
let t = r[e], n = m[e][p];
|
|
34
|
+
if (t.size) {
|
|
35
|
+
let e = s(t, [t.size]), r = e.floorDiv(n * v).mul(h[p] * v).add(e.mod(n * v)).add(y * v);
|
|
36
|
+
d.scatter(_, r, t.astype(g));
|
|
37
|
+
}
|
|
38
|
+
y += n;
|
|
39
|
+
}
|
|
40
|
+
return _;
|
|
41
|
+
} catch (e) {
|
|
42
|
+
throw _.dispose(), e;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function d(e, t = 0) {
|
|
46
|
+
return u(e, t, !1);
|
|
47
|
+
}
|
|
48
|
+
function f(e, t = 0) {
|
|
49
|
+
return u(e, t, !0);
|
|
50
|
+
}
|
|
51
|
+
function p(t, n) {
|
|
52
|
+
l(t);
|
|
53
|
+
let r = n === void 0 ? t.shape.map((e, t) => t) : (typeof n == "number" ? [n] : n).map((e) => a(e, t.ndim));
|
|
54
|
+
if (new Set(r).size !== r.length) throw e("flip axes must be distinct; remove repeated axes", "compute");
|
|
55
|
+
let i = s(t, t.shape), c = i;
|
|
56
|
+
for (let e of r) {
|
|
57
|
+
let n = o(t.shape.slice(e + 1));
|
|
58
|
+
c = c.add(i.floorDiv(n).mod(t.shape[e]).mul(-2).add(t.shape[e] - 1).mul(n));
|
|
59
|
+
}
|
|
60
|
+
return t.context.take(t, c);
|
|
61
|
+
}
|
|
62
|
+
function m(t, n) {
|
|
63
|
+
l(t);
|
|
64
|
+
let r = typeof n == "number" ? [n] : [...n];
|
|
65
|
+
if (!r.every(c)) throw e("tile repetitions must be nonnegative safe integers", "compute");
|
|
66
|
+
let i = [...t.shape];
|
|
67
|
+
for (; i.length < r.length;) i.unshift(1);
|
|
68
|
+
for (; r.length < i.length;) r.unshift(1);
|
|
69
|
+
let a = i.map((e, t) => e * r[t]), u = s(t, a), d = u.mul(0);
|
|
70
|
+
for (let e = 0; e < i.length; e++) d = d.add(u.floorDiv(o(a.slice(e + 1))).mod(a[e]).mod(i[e]).mul(o(i.slice(e + 1))));
|
|
71
|
+
return t.context.take(t, d);
|
|
72
|
+
}
|
|
73
|
+
function h(t, n, r = null) {
|
|
74
|
+
l(t);
|
|
75
|
+
let i = r === null ? null : a(r, t.ndim), o = i === null ? t.size : t.shape[i], u = typeof n == "number" ? [n] : [...n];
|
|
76
|
+
if (!u.every(c) || u.length !== 1 && u.length !== o) throw e("repeat needs one nonnegative integer or one count per element of its axis", "compute");
|
|
77
|
+
let d = u.length === 1 ? u[0] * o : u.reduce((e, t) => e + t, 0);
|
|
78
|
+
if (!c(d) || d > 2147483647) throw e("repeat result exceeds the signed index limit; split the input", "compute");
|
|
79
|
+
let f = s(t, [d]);
|
|
80
|
+
if (u.length === 1) return t.context.take(t, f.floorDiv(u[0]), i === null ? {} : { axis: i });
|
|
81
|
+
let p = new Int32Array(d), m = 0;
|
|
82
|
+
u.forEach((e, t) => {
|
|
83
|
+
p.fill(t, m, m + e), m += e;
|
|
84
|
+
});
|
|
85
|
+
let h = t.context.array(p);
|
|
86
|
+
try {
|
|
87
|
+
return t.context.take(t, h, i === null ? {} : { axis: i });
|
|
88
|
+
} finally {
|
|
89
|
+
h.dispose();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function g(n, r, i = {}) {
|
|
93
|
+
l(n);
|
|
94
|
+
let a = typeof r == "number" ? n.shape.map(() => [r, r]) : typeof r[0] == "number" ? n.shape.map(() => r) : r;
|
|
95
|
+
if (a.length !== n.ndim || a.some((e) => e.length !== 2 || !e.every(c))) throw e("pad needs nonnegative [before, after] widths for each input dimension", "compute");
|
|
96
|
+
let u = i.mode ?? "constant";
|
|
97
|
+
if (![
|
|
98
|
+
"constant",
|
|
99
|
+
"edge",
|
|
100
|
+
"wrap",
|
|
101
|
+
"reflect",
|
|
102
|
+
"symmetric"
|
|
103
|
+
].includes(u)) throw t("pad mode must be constant, edge, wrap, reflect or symmetric", "compute");
|
|
104
|
+
let d = n.shape.map((e, t) => e + a[t][0] + a[t][1]);
|
|
105
|
+
if (!o(d)) return n.context.empty(d, n.dtype);
|
|
106
|
+
if (!n.size) {
|
|
107
|
+
if (u !== "constant") throw e("Nonconstant padding needs nonempty input axes; use constant padding", "compute");
|
|
108
|
+
return n.context.full(d, i.constantValues ?? 0, n.dtype);
|
|
109
|
+
}
|
|
110
|
+
let f = s(n, d), p = f.mul(0), m = f.ge(0);
|
|
111
|
+
for (let e = 0; e < n.ndim; e++) {
|
|
112
|
+
let t = n.shape[e], r = f.floorDiv(o(d.slice(e + 1))).mod(d[e]).sub(a[e][0]);
|
|
113
|
+
m = m.and(r.ge(0)).and(r.lt(t));
|
|
114
|
+
let i = r.clip(0, t - 1);
|
|
115
|
+
if (u === "wrap") i = r.mod(t);
|
|
116
|
+
else if (u === "reflect" && t > 1) {
|
|
117
|
+
let e = 2 * (t - 1), a = r.mod(e);
|
|
118
|
+
i = n.context.where(a.lt(t), a, a.neg().add(e));
|
|
119
|
+
} else if (u === "symmetric") {
|
|
120
|
+
let e = 2 * t, a = r.mod(e);
|
|
121
|
+
i = n.context.where(a.lt(t), a, a.neg().add(e - 1));
|
|
122
|
+
}
|
|
123
|
+
p = p.add(i.mul(o(n.shape.slice(e + 1))));
|
|
124
|
+
}
|
|
125
|
+
let h = n.context.take(n, p);
|
|
126
|
+
if (u !== "constant") return h;
|
|
127
|
+
try {
|
|
128
|
+
return n.context.where(m, h, i.constantValues ?? 0).astype(n.dtype).eval();
|
|
129
|
+
} finally {
|
|
130
|
+
h.dispose();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
//#endregion
|
|
134
|
+
export { d as concatenate, p as flip, g as pad, h as repeat, f as stack, m as tile };
|
package/dist/signal.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { GpuArray } from './array.js';
|
|
2
|
+
import { Operand } from './expr.js';
|
|
3
|
+
/** Which part of the full convolution to return. */
|
|
4
|
+
export type ConvolveMode = 'full' | 'same' | 'valid';
|
|
5
|
+
/** Options for convolution. */
|
|
6
|
+
export interface ConvolveOptions {
|
|
7
|
+
/** `'full'` (every overlap, NumPy's convolve default), `'same'` (the input's size, centred) or `'valid'` (complete overlaps). */
|
|
8
|
+
readonly mode?: ConvolveMode;
|
|
9
|
+
/** `'direct'`, `'fft'`, or `'auto'` (default: direct for kernels up to 64 taps in 1-D or 15 x 15 in 2-D). */
|
|
10
|
+
readonly method?: 'auto' | 'direct' | 'fft';
|
|
11
|
+
}
|
|
12
|
+
/** WGSL for direct 1-D convolution of `lines` signals of length n with one kernel of length m. */
|
|
13
|
+
export declare const convolve1dShader = "struct Conv { n: u32, m: u32, length: u32, start: u32, lines: u32 }\n@group(0) @binding(0) var<storage, read> signal: array<f32>;\n@group(0) @binding(1) var<storage, read> taps: array<f32>;\n@group(0) @binding(2) var<storage, read_write> out: array<f32>;\n@group(0) @binding(3) var<uniform> conv: Conv;\n@compute @workgroup_size(256)\nfn main(@builtin(workgroup_id) wid: vec3u, @builtin(num_workgroups) nwg: vec3u, @builtin(local_invocation_index) lid: u32) {\n let t = (wid.x + wid.y * nwg.x) * 256u + lid;\n if (t >= conv.length * conv.lines) { return; }\n let line = t / conv.length;\n let k = t % conv.length + conv.start;\n let first = select(0u, k - (conv.m - 1u), k >= conv.m - 1u);\n let last = min(k, conv.n - 1u);\n var sum = 0.0;\n for (var j = first; j <= last; j++) { sum += signal[line * conv.n + j] * taps[k - j]; }\n out[t] = sum;\n}";
|
|
14
|
+
/** WGSL for direct 2-D convolution (zero fill) of `batch` images h x w with one kernel kh x kw. */
|
|
15
|
+
export declare const convolve2dShader = "struct Conv { h: u32, w: u32, kh: u32, kw: u32, oh: u32, ow: u32, y0: u32, x0: u32, batch: u32 }\n@group(0) @binding(0) var<storage, read> image: array<f32>;\n@group(0) @binding(1) var<storage, read> taps: array<f32>;\n@group(0) @binding(2) var<storage, read_write> out: array<f32>;\n@group(0) @binding(3) var<uniform> conv: Conv;\n@compute @workgroup_size(256)\nfn main(@builtin(workgroup_id) wid: vec3u, @builtin(num_workgroups) nwg: vec3u, @builtin(local_invocation_index) lid: u32) {\n let t = (wid.x + wid.y * nwg.x) * 256u + lid;\n if (t >= conv.oh * conv.ow * conv.batch) { return; }\n let b = t / (conv.oh * conv.ow);\n let y = (t / conv.ow) % conv.oh + conv.y0;\n let x = t % conv.ow + conv.x0;\n var sum = 0.0;\n for (var i = 0u; i < conv.kh; i++) {\n if (y < i || y - i >= conv.h) { continue; }\n for (var j = 0u; j < conv.kw; j++) {\n if (x < j || x - j >= conv.w) { continue; }\n sum += image[(b * conv.h + y - i) * conv.w + x - j] * taps[i * conv.kw + j];\n }\n }\n out[t] = sum;\n}";
|
|
16
|
+
/**
|
|
17
|
+
* 1-D convolution of `signal` (along its last axis, batched over the rest) with `kernel` (1-D), NumPy `convolve`
|
|
18
|
+
* semantics, f32.
|
|
19
|
+
* @example const smooth = convolve(series, gpu.array([0.25, 0.5, 0.25]), { mode: 'same' });
|
|
20
|
+
*/
|
|
21
|
+
export declare function convolve(signal: Operand, kernel: Operand, options?: ConvolveOptions): GpuArray;
|
|
22
|
+
/** 1-D cross-correlation (NumPy `correlate`, default mode `'valid'`): convolution with the reversed kernel. */
|
|
23
|
+
export declare function correlate(signal: Operand, kernel: Operand, options?: ConvolveOptions): GpuArray;
|
|
24
|
+
/**
|
|
25
|
+
* 2-D convolution of `image` (its last two axes, batched over the rest) with a 2-D `kernel`, zero-filled at the edges
|
|
26
|
+
* (SciPy `convolve2d` with `boundary='fill'`), f32.
|
|
27
|
+
* @example const blurred = convolve2d(image, gaussian5x5, { mode: 'same' });
|
|
28
|
+
*/
|
|
29
|
+
export declare function convolve2d(image: Operand, kernel: Operand, options?: ConvolveOptions): GpuArray;
|
package/dist/signal.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { n as e, r as t } from "./errors-CDgOgzCd.js";
|
|
2
|
+
import { n } from "./expr-t3jm8acM.js";
|
|
3
|
+
import { t as r } from "./array-C29noZq7.js";
|
|
4
|
+
import { fft as i, fft2 as a, ifft as o, ifft2 as s } from "./fft.js";
|
|
5
|
+
//#region src/signal.ts
|
|
6
|
+
var c = (e) => e instanceof r && e.dtype === "f32" && e.contiguous && e.offset === 0 ? {
|
|
7
|
+
array: e,
|
|
8
|
+
owned: !1
|
|
9
|
+
} : {
|
|
10
|
+
array: e.astype("f32").eval(),
|
|
11
|
+
owned: !0
|
|
12
|
+
};
|
|
13
|
+
function l(e, n, r) {
|
|
14
|
+
if (r === "full") return {
|
|
15
|
+
length: e + n - 1,
|
|
16
|
+
start: 0
|
|
17
|
+
};
|
|
18
|
+
if (r === "same") {
|
|
19
|
+
let t = Math.max(e, n);
|
|
20
|
+
return {
|
|
21
|
+
length: t,
|
|
22
|
+
start: Math.floor((e + n - 1 - t) / 2)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (r === "valid") return {
|
|
26
|
+
length: Math.max(e, n) - Math.min(e, n) + 1,
|
|
27
|
+
start: Math.min(e, n) - 1
|
|
28
|
+
};
|
|
29
|
+
throw t(`Convolution mode must be 'full', 'same' or 'valid', got ${String(r)}`, "compute");
|
|
30
|
+
}
|
|
31
|
+
var u = "struct Conv { n: u32, m: u32, length: u32, start: u32, lines: u32 }\n@group(0) @binding(0) var<storage, read> signal: array<f32>;\n@group(0) @binding(1) var<storage, read> taps: array<f32>;\n@group(0) @binding(2) var<storage, read_write> out: array<f32>;\n@group(0) @binding(3) var<uniform> conv: Conv;\n@compute @workgroup_size(256)\nfn main(@builtin(workgroup_id) wid: vec3u, @builtin(num_workgroups) nwg: vec3u, @builtin(local_invocation_index) lid: u32) {\n let t = (wid.x + wid.y * nwg.x) * 256u + lid;\n if (t >= conv.length * conv.lines) { return; }\n let line = t / conv.length;\n let k = t % conv.length + conv.start;\n let first = select(0u, k - (conv.m - 1u), k >= conv.m - 1u);\n let last = min(k, conv.n - 1u);\n var sum = 0.0;\n for (var j = first; j <= last; j++) { sum += signal[line * conv.n + j] * taps[k - j]; }\n out[t] = sum;\n}", d = "struct Conv { h: u32, w: u32, kh: u32, kw: u32, oh: u32, ow: u32, y0: u32, x0: u32, batch: u32 }\n@group(0) @binding(0) var<storage, read> image: array<f32>;\n@group(0) @binding(1) var<storage, read> taps: array<f32>;\n@group(0) @binding(2) var<storage, read_write> out: array<f32>;\n@group(0) @binding(3) var<uniform> conv: Conv;\n@compute @workgroup_size(256)\nfn main(@builtin(workgroup_id) wid: vec3u, @builtin(num_workgroups) nwg: vec3u, @builtin(local_invocation_index) lid: u32) {\n let t = (wid.x + wid.y * nwg.x) * 256u + lid;\n if (t >= conv.oh * conv.ow * conv.batch) { return; }\n let b = t / (conv.oh * conv.ow);\n let y = (t / conv.ow) % conv.oh + conv.y0;\n let x = t % conv.ow + conv.x0;\n var sum = 0.0;\n for (var i = 0u; i < conv.kh; i++) {\n if (y < i || y - i >= conv.h) { continue; }\n for (var j = 0u; j < conv.kw; j++) {\n if (x < j || x - j >= conv.w) { continue; }\n sum += image[(b * conv.h + y - i) * conv.w + x - j] * taps[i * conv.kw + j];\n }\n }\n out[t] = sum;\n}";
|
|
32
|
+
function f(r, a, s = {}) {
|
|
33
|
+
if (!(r instanceof n) || !(a instanceof n)) throw t("convolve takes two GpuArrays or Exprs", "compute");
|
|
34
|
+
if (a.ndim !== 1) throw e("convolve takes a 1-D kernel; use convolve2d for images", "compute");
|
|
35
|
+
if (r.ndim < 1) throw e("convolve needs a signal of at least one dimension", "compute");
|
|
36
|
+
let d = r.context;
|
|
37
|
+
d.check();
|
|
38
|
+
let f = r.shape[r.ndim - 1], p = a.size;
|
|
39
|
+
if (f === 0 || p === 0) throw e("convolve needs non-empty signal and kernel", "compute");
|
|
40
|
+
let { length: m, start: h } = l(f, p, s.mode ?? "full"), g = r.size / f, _ = [...r.shape.slice(0, -1), m], v = s.method ?? "auto";
|
|
41
|
+
if (v === "fft" || v === "auto" && Math.min(f, p) > 64) {
|
|
42
|
+
let e = 1;
|
|
43
|
+
for (; e < f + p - 1;) e *= 2;
|
|
44
|
+
let t = (t, n, r) => {
|
|
45
|
+
let { array: i, owned: a } = c(t), o = d.zeros([r, e]);
|
|
46
|
+
return d.submit([], Array.from({ length: r }, (t, r) => [
|
|
47
|
+
i.buffer,
|
|
48
|
+
r * n * 4,
|
|
49
|
+
o.buffer,
|
|
50
|
+
r * e * 4,
|
|
51
|
+
n * 4
|
|
52
|
+
])), a && i.dispose(), o;
|
|
53
|
+
}, n = t(r, f, g), s = t(a, p, 1), l = i(n), u = i(s), v = l.re.mul(u.re).sub(l.im.mul(u.im)).eval(), y = l.re.mul(u.im).add(l.im.mul(u.re)).eval(), b = o({
|
|
54
|
+
re: v,
|
|
55
|
+
im: y
|
|
56
|
+
}), x = b.re.slice(null, [h, h + m]).eval().transfer(_);
|
|
57
|
+
for (let e of [
|
|
58
|
+
n,
|
|
59
|
+
s,
|
|
60
|
+
l.re,
|
|
61
|
+
l.im,
|
|
62
|
+
u.re,
|
|
63
|
+
u.im,
|
|
64
|
+
v,
|
|
65
|
+
y,
|
|
66
|
+
b.re,
|
|
67
|
+
b.im
|
|
68
|
+
]) e.dispose();
|
|
69
|
+
return x;
|
|
70
|
+
}
|
|
71
|
+
let y = c(r), b = c(a), x = d.empty(_, "f32");
|
|
72
|
+
return d.submit([{
|
|
73
|
+
program: d.program(u, void 0, "convolve"),
|
|
74
|
+
resources: {
|
|
75
|
+
signal: y.array,
|
|
76
|
+
taps: b.array,
|
|
77
|
+
out: x,
|
|
78
|
+
conv: new Uint32Array([
|
|
79
|
+
f,
|
|
80
|
+
p,
|
|
81
|
+
m,
|
|
82
|
+
h,
|
|
83
|
+
g,
|
|
84
|
+
0,
|
|
85
|
+
0,
|
|
86
|
+
0
|
|
87
|
+
])
|
|
88
|
+
},
|
|
89
|
+
workgroups: d.grid(Math.ceil(m * g / 256))
|
|
90
|
+
}]), y.owned && y.array.dispose(), b.owned && b.array.dispose(), x;
|
|
91
|
+
}
|
|
92
|
+
function p(e, t, n = {}) {
|
|
93
|
+
let i = t instanceof r ? t : t.eval(), a = f(e, i.slice([
|
|
94
|
+
null,
|
|
95
|
+
null,
|
|
96
|
+
-1
|
|
97
|
+
]), {
|
|
98
|
+
mode: "valid",
|
|
99
|
+
...n
|
|
100
|
+
});
|
|
101
|
+
return i !== t && i.dispose(), a;
|
|
102
|
+
}
|
|
103
|
+
function m(r, i, o = {}) {
|
|
104
|
+
if (!(r instanceof n) || !(i instanceof n)) throw t("convolve2d takes two GpuArrays or Exprs", "compute");
|
|
105
|
+
if (i.ndim !== 2 || r.ndim < 2) throw e("convolve2d takes an image of at least 2 dimensions and a 2-D kernel", "compute");
|
|
106
|
+
let u = r.context;
|
|
107
|
+
u.check();
|
|
108
|
+
let [f, p] = r.shape.slice(-2), [m, h] = i.shape;
|
|
109
|
+
if (!f || !p || !m || !h) throw e("convolve2d needs non-empty image and kernel", "compute");
|
|
110
|
+
let g = o.mode ?? "full", _ = l(f, m, g), v = l(p, h, g), y = r.size / (f * p), b = [
|
|
111
|
+
...r.shape.slice(0, -2),
|
|
112
|
+
_.length,
|
|
113
|
+
v.length
|
|
114
|
+
], x = o.method ?? "auto";
|
|
115
|
+
if (x === "fft" || x === "auto" && m * h > 225) {
|
|
116
|
+
let e = (e) => {
|
|
117
|
+
let t = 1;
|
|
118
|
+
for (; t < e;) t *= 2;
|
|
119
|
+
return t;
|
|
120
|
+
}, t = e(f + m - 1), n = e(p + h - 1), o = (e, r, i, a) => {
|
|
121
|
+
let { array: o, owned: s } = c(e), l = u.zeros([
|
|
122
|
+
r,
|
|
123
|
+
t,
|
|
124
|
+
n
|
|
125
|
+
]), d = [];
|
|
126
|
+
for (let e = 0; e < r; e++) for (let r = 0; r < i; r++) d.push([
|
|
127
|
+
o.buffer,
|
|
128
|
+
(e * i + r) * a * 4,
|
|
129
|
+
l.buffer,
|
|
130
|
+
(e * t + r) * n * 4,
|
|
131
|
+
a * 4
|
|
132
|
+
]);
|
|
133
|
+
return u.submit([], d), s && o.dispose(), l;
|
|
134
|
+
}, l = o(r, y, f, p), d = o(i, 1, m, h), g = a(l), x = a(d), S = g.re.mul(x.re).sub(g.im.mul(x.im)).eval(), C = g.re.mul(x.im).add(g.im.mul(x.re)).eval(), w = s({
|
|
135
|
+
re: S,
|
|
136
|
+
im: C
|
|
137
|
+
}), T = w.re.slice(null, [_.start, _.start + _.length], [v.start, v.start + v.length]).eval().transfer(b);
|
|
138
|
+
for (let e of [
|
|
139
|
+
l,
|
|
140
|
+
d,
|
|
141
|
+
g.re,
|
|
142
|
+
g.im,
|
|
143
|
+
x.re,
|
|
144
|
+
x.im,
|
|
145
|
+
S,
|
|
146
|
+
C,
|
|
147
|
+
w.re,
|
|
148
|
+
w.im
|
|
149
|
+
]) e.dispose();
|
|
150
|
+
return T;
|
|
151
|
+
}
|
|
152
|
+
let S = c(r), C = c(i), w = u.empty(b, "f32"), T = _.length * v.length * y;
|
|
153
|
+
return T > 0 && u.submit([{
|
|
154
|
+
program: u.program(d, void 0, "convolve2d"),
|
|
155
|
+
resources: {
|
|
156
|
+
image: S.array,
|
|
157
|
+
taps: C.array,
|
|
158
|
+
out: w,
|
|
159
|
+
conv: new Uint32Array([
|
|
160
|
+
f,
|
|
161
|
+
p,
|
|
162
|
+
m,
|
|
163
|
+
h,
|
|
164
|
+
_.length,
|
|
165
|
+
v.length,
|
|
166
|
+
_.start,
|
|
167
|
+
v.start,
|
|
168
|
+
y,
|
|
169
|
+
0,
|
|
170
|
+
0,
|
|
171
|
+
0
|
|
172
|
+
])
|
|
173
|
+
},
|
|
174
|
+
workgroups: u.grid(Math.ceil(T / 256))
|
|
175
|
+
}]), S.owned && S.array.dispose(), C.owned && C.array.dispose(), w;
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
export { f as convolve, u as convolve1dShader, m as convolve2d, d as convolve2dShader, p as correlate };
|
package/dist/sort.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { GpuArray } from './array.js';
|
|
2
|
+
import { Operand } from './expr.js';
|
|
3
|
+
import type { ComputeContext, WaitOptions } from './context.js';
|
|
4
|
+
/** Options for sorting. */
|
|
5
|
+
export interface SortOptions {
|
|
6
|
+
/** Largest first. Default false. */
|
|
7
|
+
readonly descending?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Low key bits to sort on, rounded up to a multiple of 4 (one radix pass each). Default 32; fewer for u32 keys
|
|
10
|
+
* known to be small (`bits: 16` for keys below 65,536 runs half the passes).
|
|
11
|
+
*/
|
|
12
|
+
readonly bits?: number;
|
|
13
|
+
}
|
|
14
|
+
/** Sorted copy (see ComputeContext.sort). */
|
|
15
|
+
export declare function sort(ctx: ComputeContext, input: Operand, options?: SortOptions): GpuArray;
|
|
16
|
+
/** Indices that sort (see ComputeContext.argsort). */
|
|
17
|
+
export declare function argsort(ctx: ComputeContext, input: Operand, options?: SortOptions): GpuArray<'u32'>;
|
|
18
|
+
/** Sort keys moving values (see ComputeContext.sortByKey). */
|
|
19
|
+
export declare function sortByKey(ctx: ComputeContext, keys: Operand, values: Operand, options?: SortOptions): {
|
|
20
|
+
keys: GpuArray;
|
|
21
|
+
values: GpuArray;
|
|
22
|
+
};
|
|
23
|
+
/** The k largest or smallest (see ComputeContext.topk). */
|
|
24
|
+
export declare function topk(ctx: ComputeContext, input: Operand, k: number, options?: {
|
|
25
|
+
readonly largest?: boolean;
|
|
26
|
+
}): {
|
|
27
|
+
values: GpuArray;
|
|
28
|
+
indices: GpuArray<'u32'>;
|
|
29
|
+
};
|
|
30
|
+
/** Quantiles with linear interpolation (see ComputeContext.quantile). */
|
|
31
|
+
export declare function quantile(ctx: ComputeContext, input: Operand, q: number | readonly number[]): GpuArray<'f32'>;
|
|
32
|
+
/** Distinct values and counts (see ComputeContext.unique). */
|
|
33
|
+
export declare function unique(ctx: ComputeContext, input: Operand, options?: {
|
|
34
|
+
readonly counts?: boolean;
|
|
35
|
+
} & WaitOptions): Promise<{
|
|
36
|
+
values: GpuArray;
|
|
37
|
+
counts?: GpuArray<'u32'>;
|
|
38
|
+
}>;
|
package/dist/sparse.d.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import { GpuArray } from './array.js';
|
|
3
|
+
import { Operand } from './expr.js';
|
|
4
|
+
import { type ComputeContext, type WaitOptions } from './context.js';
|
|
5
|
+
/** WGSL for y = A x, one thread per row. */
|
|
6
|
+
export declare const spmvShader = "struct Shape { rows: u32, pad0: u32, pad1: u32, pad2: u32 }\n@group(0) @binding(0) var<storage, read> rowPtr: array<u32>;\n@group(0) @binding(1) var<storage, read> colIdx: array<u32>;\n@group(0) @binding(2) var<storage, read> values: array<f32>;\n@group(0) @binding(3) var<storage, read> x: array<f32>;\n@group(0) @binding(4) var<storage, read_write> y: array<f32>;\n@group(0) @binding(5) var<uniform> shape: Shape;\n@compute @workgroup_size(256)\nfn main(@builtin(workgroup_id) wid: vec3u, @builtin(num_workgroups) nwg: vec3u, @builtin(local_invocation_index) lid: u32) {\n let r = (wid.x + wid.y * nwg.x) * 256u + lid;\n if (r >= shape.rows) { return; }\n var sum = 0.0;\n for (var k = rowPtr[r]; k < rowPtr[r + 1u]; k++) { sum += values[k] * x[colIdx[k]]; }\n y[r] = sum;\n}";
|
|
7
|
+
/** WGSL for the diagonal: the value at column r of row r (0 where none is stored; duplicates were summed). */
|
|
8
|
+
export declare const diagonalShader = "struct Shape { rows: u32, pad0: u32, pad1: u32, pad2: u32 }\n@group(0) @binding(0) var<storage, read> rowPtr: array<u32>;\n@group(0) @binding(1) var<storage, read> colIdx: array<u32>;\n@group(0) @binding(2) var<storage, read> values: array<f32>;\n@group(0) @binding(3) var<storage, read_write> d: array<f32>;\n@group(0) @binding(4) var<uniform> shape: Shape;\n@compute @workgroup_size(256)\nfn main(@builtin(workgroup_id) wid: vec3u, @builtin(num_workgroups) nwg: vec3u, @builtin(local_invocation_index) lid: u32) {\n let r = (wid.x + wid.y * nwg.x) * 256u + lid;\n if (r >= shape.rows) { return; }\n var v = 0.0;\n for (var k = rowPtr[r]; k < rowPtr[r + 1u]; k++) { if (colIdx[k] == r) { v += values[k]; } }\n d[r] = v;\n}";
|
|
9
|
+
/**
|
|
10
|
+
* A sparse matrix in compressed sparse row form on the GPU: `rowPtr` (rows + 1 offsets), and per stored entry its
|
|
11
|
+
* column (`colIdx`), row (`rowIdx`) and value (`values`, f32). Columns are ascending within each row and duplicates
|
|
12
|
+
* are summed. Make one with {@link csr}, {@link coo} or {@link diags}; dispose it to free its arrays.
|
|
13
|
+
*/
|
|
14
|
+
export declare class CsrMatrix {
|
|
15
|
+
#private;
|
|
16
|
+
/** The context the arrays belong to. */
|
|
17
|
+
readonly context: ComputeContext;
|
|
18
|
+
/** Row offsets (u32, rows + 1): row r's entries are at [rowPtr[r], rowPtr[r + 1]). */
|
|
19
|
+
readonly rowPtr: GpuArray<'u32'>;
|
|
20
|
+
/** Column of each entry (u32, nnz). */
|
|
21
|
+
readonly colIdx: GpuArray<'u32'>;
|
|
22
|
+
/** Row of each entry (u32, nnz), the expanded rowPtr. */
|
|
23
|
+
readonly rowIdx: GpuArray<'u32'>;
|
|
24
|
+
/** Value of each entry (f32, nnz). */
|
|
25
|
+
readonly values: GpuArray<'f32'>;
|
|
26
|
+
/** [rows, cols]. */
|
|
27
|
+
readonly shape: readonly [number, number];
|
|
28
|
+
/** Stored entries. */
|
|
29
|
+
readonly nnz: number;
|
|
30
|
+
/** @internal Use csr(), coo() or diags(). */
|
|
31
|
+
constructor(
|
|
32
|
+
/** The context the arrays belong to. */
|
|
33
|
+
context: ComputeContext, shape: readonly [number, number],
|
|
34
|
+
/** Row offsets (u32, rows + 1): row r's entries are at [rowPtr[r], rowPtr[r + 1]). */
|
|
35
|
+
rowPtr: GpuArray<'u32'>,
|
|
36
|
+
/** Column of each entry (u32, nnz). */
|
|
37
|
+
colIdx: GpuArray<'u32'>,
|
|
38
|
+
/** Row of each entry (u32, nnz), the expanded rowPtr. */
|
|
39
|
+
rowIdx: GpuArray<'u32'>,
|
|
40
|
+
/** Value of each entry (f32, nnz). */
|
|
41
|
+
values: GpuArray<'f32'>);
|
|
42
|
+
/** True after `dispose()`. */
|
|
43
|
+
get disposed(): boolean;
|
|
44
|
+
/** `A x` for a vector of `cols` elements, f32. */
|
|
45
|
+
matvec(x: Operand): GpuArray<'f32'>;
|
|
46
|
+
/** The main diagonal (min(rows, cols) elements, f32; 0 where nothing is stored). */
|
|
47
|
+
diagonal(): GpuArray<'f32'>;
|
|
48
|
+
/** The transpose, as a new CSR matrix built on the GPU (a stable sort of the entries by column). */
|
|
49
|
+
transpose(): CsrMatrix;
|
|
50
|
+
/** The matrix as a dense rows x cols f32 array. */
|
|
51
|
+
toDense(): GpuArray<'f32'>;
|
|
52
|
+
/** Frees the four arrays. */
|
|
53
|
+
dispose(): void;
|
|
54
|
+
/** Same as `dispose()`, for `using`. */
|
|
55
|
+
[Symbol.dispose](): void;
|
|
56
|
+
}
|
|
57
|
+
/** Entries of a sparse matrix by coordinates. */
|
|
58
|
+
export interface CooData {
|
|
59
|
+
/** Row of each entry. */
|
|
60
|
+
readonly rows: ArrayLike<number>;
|
|
61
|
+
/** Column of each entry. */
|
|
62
|
+
readonly cols: ArrayLike<number>;
|
|
63
|
+
/** Value of each entry. */
|
|
64
|
+
readonly values: ArrayLike<number>;
|
|
65
|
+
/** [rows, cols] of the matrix. */
|
|
66
|
+
readonly shape: readonly [number, number];
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A CSR matrix from coordinates (SciPy `coo_matrix((values, (rows, cols))).tocsr()`): entries in any order,
|
|
70
|
+
* duplicates summed. Built on the CPU from your arrays, then uploaded.
|
|
71
|
+
* @example const A = coo(gpu, { rows: [0, 1, 1], cols: [0, 0, 1], values: [2, -1, 2], shape: [2, 2] });
|
|
72
|
+
*/
|
|
73
|
+
export declare function coo(gpu: ComputeContext, data: CooData): CsrMatrix;
|
|
74
|
+
/** A CSR matrix in its own arrays. */
|
|
75
|
+
export interface CsrData {
|
|
76
|
+
/** Row offsets, rows + 1 of them, starting at 0 and never decreasing. */
|
|
77
|
+
readonly rowPtr: ArrayLike<number>;
|
|
78
|
+
/** Column of each entry. */
|
|
79
|
+
readonly colIdx: ArrayLike<number>;
|
|
80
|
+
/** Value of each entry. */
|
|
81
|
+
readonly values: ArrayLike<number>;
|
|
82
|
+
/** [rows, cols] of the matrix. */
|
|
83
|
+
readonly shape: readonly [number, number];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A CSR matrix from its arrays (SciPy `csr_matrix((values, colIdx, rowPtr))`). Columns may come in any order within a
|
|
87
|
+
* row and repeat; they are sorted and summed.
|
|
88
|
+
* @example const I = csr(gpu, { rowPtr: [0, 1, 2], colIdx: [0, 1], values: [1, 1], shape: [2, 2] });
|
|
89
|
+
*/
|
|
90
|
+
export declare function csr(gpu: ComputeContext, data: CsrData): CsrMatrix;
|
|
91
|
+
/**
|
|
92
|
+
* A matrix with constant diagonals (SciPy `diags`): `values[k]` along the diagonal at `offsets[k]` (0 the main
|
|
93
|
+
* diagonal, positive above, negative below).
|
|
94
|
+
* @example const poisson = diags(gpu, [-1, 2, -1], [-1, 0, 1], [n, n]); // the 1-D Laplacian
|
|
95
|
+
*/
|
|
96
|
+
export declare function diags(gpu: ComputeContext, values: readonly number[], offsets: readonly number[], shape: readonly [number, number]): CsrMatrix;
|
|
97
|
+
/** `A x` (see {@link CsrMatrix.matvec}). */
|
|
98
|
+
export declare function spmv(A: CsrMatrix, x: Operand): GpuArray<'f32'>;
|
|
99
|
+
/** Options for the iterative solvers. */
|
|
100
|
+
export interface SparseSolveOptions extends WaitOptions {
|
|
101
|
+
/** Starting guess. Default zeros. */
|
|
102
|
+
readonly x0?: Operand;
|
|
103
|
+
/** Stop when the residual norm is at most `tol` times the norm of b. Default 1e-5 (f32 arithmetic). */
|
|
104
|
+
readonly tol?: number;
|
|
105
|
+
/** Or at most `atol`. Default 0. */
|
|
106
|
+
readonly atol?: number;
|
|
107
|
+
/** Most iterations. Default 10 times the number of rows. */
|
|
108
|
+
readonly maxIterations?: number;
|
|
109
|
+
/** `'jacobi'` (divide by the diagonal) or `'none'`. Default `'jacobi'`. */
|
|
110
|
+
readonly preconditioner?: 'jacobi' | 'none';
|
|
111
|
+
/** Iterations between residual read-backs (the only GPU-to-CPU traffic). Default 32. */
|
|
112
|
+
readonly checkEvery?: number;
|
|
113
|
+
}
|
|
114
|
+
/** What a solver found. */
|
|
115
|
+
export interface SparseSolveResult {
|
|
116
|
+
/** The solution (f32, the size of b). */
|
|
117
|
+
readonly x: GpuArray<'f32'>;
|
|
118
|
+
/** Iterations run. */
|
|
119
|
+
readonly iterations: number;
|
|
120
|
+
/** Final residual norm ||b - A x||, recomputed from x. */
|
|
121
|
+
readonly residual: number;
|
|
122
|
+
/** True when the residual met the tolerance. */
|
|
123
|
+
readonly converged: boolean;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Solves A x = b for a symmetric positive definite A by the conjugate gradient method, on the GPU. Resolves the
|
|
127
|
+
* solution with its iteration count, residual and whether it converged (it does not reject when it did not).
|
|
128
|
+
* @example const { x, iterations } = await cg(A, b, { tol: 1e-6 });
|
|
129
|
+
*/
|
|
130
|
+
export declare function cg(A: CsrMatrix, b: Operand, options?: SparseSolveOptions): Promise<SparseSolveResult>;
|
|
131
|
+
/**
|
|
132
|
+
* Solves A x = b for a general square A by BiCGSTAB, on the GPU. Resolves the solution with its iteration count,
|
|
133
|
+
* residual and whether it converged; a breakdown (a zero inner product) ends early with `converged: false`.
|
|
134
|
+
* @example const { x, converged } = await bicgstab(A, b);
|
|
135
|
+
*/
|
|
136
|
+
export declare function bicgstab(A: CsrMatrix, b: Operand, options?: SparseSolveOptions): Promise<SparseSolveResult>;
|