@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.
Files changed (75) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +804 -2
  4. package/dist/array-C29noZq7.js +166 -0
  5. package/dist/array.d.ts +109 -0
  6. package/dist/autodiff.d.ts +22 -0
  7. package/dist/autodiff.js +143 -0
  8. package/dist/autotune.d.ts +100 -0
  9. package/dist/autotune.js +179 -0
  10. package/dist/compat-YK57ZCbA.js +4 -0
  11. package/dist/compat.d.ts +2 -0
  12. package/dist/context-CCvs3wkC.js +2479 -0
  13. package/dist/context.d.ts +376 -0
  14. package/dist/cpu-D4UQwJ9L.js +871 -0
  15. package/dist/cpu-kernel.wasm +0 -0
  16. package/dist/cpu-load-XFcM1TXj.js +5 -0
  17. package/dist/cpu-load.d.ts +3 -0
  18. package/dist/cpu-runtime.d.ts +8 -0
  19. package/dist/cpu-wasm.d.ts +42 -0
  20. package/dist/cpu-wasm.js +161 -0
  21. package/dist/cpu.d.ts +138 -0
  22. package/dist/decomp-kernel.wasm +0 -0
  23. package/dist/decomp.d.ts +42 -0
  24. package/dist/decomp.js +158 -0
  25. package/dist/distance.d.ts +85 -0
  26. package/dist/distance.js +256 -0
  27. package/dist/dtype.d.ts +36 -0
  28. package/dist/errors-CDgOgzCd.js +7 -0
  29. package/dist/expr-t3jm8acM.js +996 -0
  30. package/dist/expr.d.ts +196 -0
  31. package/dist/fft.d.ts +33 -0
  32. package/dist/fft.js +168 -0
  33. package/dist/frame.d.ts +76 -0
  34. package/dist/frame.js +85 -0
  35. package/dist/graph.d.ts +75 -0
  36. package/dist/graph.js +174 -0
  37. package/dist/histogram.d.ts +17 -0
  38. package/dist/index.d.ts +17 -0
  39. package/dist/index.js +4 -0
  40. package/dist/kernel.d.ts +65 -0
  41. package/dist/linalg.d.ts +21 -0
  42. package/dist/linalg.js +149 -0
  43. package/dist/nn-extra.d.ts +130 -0
  44. package/dist/nn-extra.js +322 -0
  45. package/dist/nn.d.ts +385 -0
  46. package/dist/nn.js +704 -0
  47. package/dist/onnx-image-D2qfYsXf.js +66 -0
  48. package/dist/onnx-image.d.ts +10 -0
  49. package/dist/onnx-kernel.wasm +0 -0
  50. package/dist/onnx-regex-DaesIdrm.js +331 -0
  51. package/dist/onnx-regex.d.ts +2 -0
  52. package/dist/onnx.d.ts +167 -0
  53. package/dist/onnx.js +885 -0
  54. package/dist/quant.d.ts +59 -0
  55. package/dist/quant.js +242 -0
  56. package/dist/random.d.ts +64 -0
  57. package/dist/random.js +150 -0
  58. package/dist/reduce.d.ts +34 -0
  59. package/dist/scan.d.ts +26 -0
  60. package/dist/select.d.ts +38 -0
  61. package/dist/shaders.d.ts +80 -0
  62. package/dist/shape.d.ts +23 -0
  63. package/dist/shape.js +134 -0
  64. package/dist/signal.d.ts +29 -0
  65. package/dist/signal.js +178 -0
  66. package/dist/sort.d.ts +38 -0
  67. package/dist/sparse.d.ts +136 -0
  68. package/dist/sparse.js +273 -0
  69. package/dist/stream.d.ts +39 -0
  70. package/dist/stream.js +100 -0
  71. package/dist/subgroups.d.ts +27 -0
  72. package/dist/subgroups.js +191 -0
  73. package/dist/usingCtx-igdMnAhr.js +57 -0
  74. package/dist/wgsl.d.ts +68 -0
  75. package/package.json +146 -4
@@ -0,0 +1,256 @@
1
+ import { n as e, r as t } from "./errors-CDgOgzCd.js";
2
+ import { n } from "./expr-t3jm8acM.js";
3
+ //#region ../shared/distance-exact.js
4
+ var r = /* @__PURE__ */ new Float64Array(), i = /* @__PURE__ */ new Float64Array(), a = /* @__PURE__ */ new Int32Array(), o = /* @__PURE__ */ new Int32Array(), s = /* @__PURE__ */ new Int32Array(), c = /* @__PURE__ */ new Float64Array();
5
+ function l(e) {
6
+ r.length >= e || (r = new Float64Array(e), i = new Float64Array(e), a = new Int32Array(e), o = new Int32Array(e), s = new Int32Array(e), c = new Float64Array(e + 1));
7
+ }
8
+ function u(e) {
9
+ let t = 0;
10
+ s[0] = 0, c[0] = -Infinity, c[1] = Infinity;
11
+ for (let n = 1; n < e; n++) {
12
+ let e = r[n] + n * n, i = s[t], a = (e - (r[i] + i * i)) / (2 * (n - i));
13
+ for (; a <= c[t];) t--, i = s[t], a = (e - (r[i] + i * i)) / (2 * (n - i));
14
+ t++, s[t] = n, c[t] = a, c[t + 1] = Infinity;
15
+ }
16
+ for (let t = 0, n = 0; t < e; t++) {
17
+ for (; c[n + 1] < t;) n++;
18
+ let e = s[n];
19
+ i[t] = (t - e) * (t - e) + r[e], a[t] = e;
20
+ }
21
+ }
22
+ function d(e, t, n) {
23
+ let s = t.length, c = 1;
24
+ for (let e of t) c *= e;
25
+ if (c === 0) return e;
26
+ if (l(Math.max(...t)), n) for (let e = 0; e < c; e++) n[e] = e;
27
+ let d = [];
28
+ for (let e = 0, n = 1; e < s; e++, n *= t[e - 1]) d.push(n);
29
+ for (let l = s - 1; l >= 0; l--) {
30
+ let s = t[l], f = d[l];
31
+ for (let t = 0; t < c; t += s * f) for (let c = 0; c < f; c++) {
32
+ let l = t + c;
33
+ for (let t = 0, n = l; t < s; t++, n += f) r[t] = e[n];
34
+ if (u(s), n) for (let e = 0; e < s; e++) o[e] = n[l + a[e] * f];
35
+ for (let t = 0, r = l; t < s; t++, r += f) e[r] = i[t], n && (n[r] = o[t]);
36
+ }
37
+ }
38
+ return e;
39
+ }
40
+ //#endregion
41
+ //#region src/distance.ts
42
+ var f = 4294967295, p = 34028234663852886e22, m = {
43
+ 2: 4096,
44
+ 3: 512
45
+ }, h = (t) => {
46
+ throw e(t, "compute");
47
+ };
48
+ function g(e, t = "jfa+1") {
49
+ (!Number.isInteger(e) || e < 1) && h(`Longest side must be a whole number of at least 1, got ${e}`);
50
+ let n = 1;
51
+ for (; n < e;) n *= 2;
52
+ let r = [];
53
+ for (let e = n / 2; e >= 1; e /= 2) r.push(e);
54
+ return t === "jfa" ? r : t === "1+jfa" ? [1, ...r] : t === "jfa+1" ? [...r, 1] : t === "jfa+2" ? [
55
+ ...r,
56
+ 2,
57
+ 1
58
+ ] : h(`Unknown flood variant "${String(t)}"; use jfa, 1+jfa, jfa+1 or jfa+2`);
59
+ }
60
+ function _(e, t, n) {
61
+ let r = e.length;
62
+ r !== 2 && r !== 3 && h(`A distance field needs a 2D or 3D shape, got ${r} dimensions`);
63
+ let i = m[r];
64
+ for (let t of e) (!Number.isInteger(t) || t < 2 || t > i) && h(`Distance field sides must be whole numbers from 2 to ${i} in ${r}D, got [${e.join(", ")}]`);
65
+ let a = e.reduce((e, t) => e * t, 1);
66
+ n !== void 0 && a * 4 > n && h(`A ${e.join(" x ")} field needs ${a * 4} bytes per buffer, over this device's storage binding limit of ${n}; use fewer cells`);
67
+ let o = t.spacing ?? 1;
68
+ return (!(o > 0) || !Number.isFinite(o)) && h(`Spacing must be a positive finite number, got ${o}`), {
69
+ cells: a,
70
+ spacing: o,
71
+ steps: g(Math.max(...e), t.variant),
72
+ dims: [
73
+ e[0],
74
+ e[1],
75
+ e[2] ?? 1
76
+ ]
77
+ };
78
+ }
79
+ var v = "struct P { w: u32, h: u32, d: u32, step: u32, mode: u32, inv: u32, spacing: f32, pad: u32 }\n@group(0) @binding(0) var<storage, read> mask: array<u32>;\n@group(0) @binding(1) var<storage, read> src: array<u32>;\n@group(0) @binding(2) var<storage, read_write> dst: array<u32>;\n@group(0) @binding(3) var<storage, read_write> out: array<u32>;\n@group(0) @binding(4) var<uniform> p: P;\nconst NONE = 0xffffffffu;\nfn squared(c: u32, x: u32, y: u32, z: u32) -> u32 {\n let cx = i32(c % p.w) - i32(x);\n let cy = i32((c / p.w) % p.h) - i32(y);\n let cz = i32(c / (p.w * p.h)) - i32(z);\n return u32(cx * cx + cy * cy + cz * cz);\n}\n@compute @workgroup_size(8, 8, 1)\nfn main(@builtin(global_invocation_id) id: vec3u) {\n if (id.x >= p.w || id.y >= p.h || id.z >= p.d) { return; }\n let i = (id.z * p.h + id.y) * p.w + id.x;\n if (p.mode == 0u) {\n dst[i] = select(NONE, i, (mask[i] != 0u) != (p.inv == 1u));\n } else if (p.mode == 1u) {\n var best = NONE;\n var bd = NONE;\n let s = i32(p.step);\n let lz = select(0, 1, p.d > 1u);\n for (var oz = -lz; oz <= lz; oz++) {\n let z = i32(id.z) + oz * s;\n if (z < 0 || z >= i32(p.d)) { continue; }\n for (var oy = -1; oy <= 1; oy++) {\n let y = i32(id.y) + oy * s;\n if (y < 0 || y >= i32(p.h)) { continue; }\n for (var ox = -1; ox <= 1; ox++) {\n let x = i32(id.x) + ox * s;\n if (x < 0 || x >= i32(p.w)) { continue; }\n let c = src[(u32(z) * p.h + u32(y)) * p.w + u32(x)];\n if (c == NONE) { continue; }\n let q = squared(c, id.x, id.y, id.z);\n if (q < bd || (q == bd && c < best)) { bd = q; best = c; }\n }\n }\n }\n dst[i] = best;\n } else {\n let c = src[i];\n if (c == NONE) { out[i] = select(NONE, 0x7f7fffffu, p.mode == 2u); }\n else if (p.mode == 3u) { out[i] = mask[c] - 1u; }\n else { out[i] = bitcast<u32>(sqrt(f32(squared(c, id.x, id.y, id.z))) * p.spacing); }\n }\n}";
80
+ function y(e, t, n, r = !1) {
81
+ let [i, a, o] = t, s = i * a * o, c = new Uint32Array(s).fill(f), l = new Uint32Array(s);
82
+ for (let t = 0; t < s; t++) e[t] !== 0 !== r && (c[t] = t);
83
+ let u = +(o > 1);
84
+ for (let e of n) {
85
+ for (let t = 0, n = 0; t < o; t++) for (let r = 0; r < a; r++) for (let s = 0; s < i; s++, n++) {
86
+ let d = f, p = f;
87
+ for (let n = -u; n <= u; n++) {
88
+ let l = t + n * e;
89
+ if (!(l < 0 || l >= o)) for (let n = -1; n <= 1; n++) {
90
+ let o = r + n * e;
91
+ if (!(o < 0 || o >= a)) for (let n = -1; n <= 1; n++) {
92
+ let u = s + n * e;
93
+ if (u < 0 || u >= i) continue;
94
+ let f = c[(l * a + o) * i + u];
95
+ if (f === 4294967295) continue;
96
+ let m = f % i - s, h = Math.floor(f / i) % a - r, g = Math.floor(f / (i * a)) - t, _ = m * m + h * h + g * g;
97
+ (_ < p || _ === p && f < d) && (p = _, d = f);
98
+ }
99
+ }
100
+ }
101
+ l[n] = d;
102
+ }
103
+ [c, l] = [l, c];
104
+ }
105
+ return c;
106
+ }
107
+ async function b(e, t, n, r, i, a) {
108
+ e.check();
109
+ let o = e.backend !== "cpu", { cells: s, spacing: c, steps: l, dims: u } = _(n, r, o ? Math.min(e.device.limits.maxStorageBufferBindingSize, e.device.limits.maxBufferSize) : void 0);
110
+ r.signal?.throwIfAborted();
111
+ let d = e.empty([...n], "f32"), m = a ? e.empty([...n], "u32") : void 0;
112
+ if (!o) {
113
+ let a = await t.read({ signal: r.signal }), o = y(a, u, l, i), h = new Float32Array(s), g = new Uint32Array(s);
114
+ for (let e = 0; e < s; e++) {
115
+ let t = o[e];
116
+ if (t === 4294967295) {
117
+ h[e] = p, g[e] = f;
118
+ continue;
119
+ }
120
+ let n = t % u[0] - e % u[0], r = Math.floor(t / u[0]) % u[1] - Math.floor(e / u[0]) % u[1], i = Math.floor(t / (u[0] * u[1])) - Math.floor(e / (u[0] * u[1]));
121
+ h[e] = Math.fround(Math.sqrt(n * n + r * r + i * i) * c), g[e] = a[t] - 1;
122
+ }
123
+ return d.write(h), m?.write(g), {
124
+ nearest: e.array(o, {
125
+ shape: [...n],
126
+ dtype: "u32"
127
+ }),
128
+ distance: d,
129
+ ids: m,
130
+ passes: l.length
131
+ };
132
+ }
133
+ let h = e.empty([...n], "u32"), g = e.empty([...n], "u32"), b = e.program(v, void 0, "distance.flood"), x = (e, t) => {
134
+ let n = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(32));
135
+ return [
136
+ u[0],
137
+ u[1],
138
+ u[2],
139
+ e,
140
+ t,
141
+ +!!i
142
+ ].forEach((e, t) => n.setUint32(4 * t, e, !0)), n.setFloat32(24, c, !0), new Uint8Array(n.buffer);
143
+ }, S = [
144
+ Math.ceil(u[0] / 8),
145
+ Math.ceil(u[1] / 8),
146
+ u[2]
147
+ ], C = (e, n, r, i, a) => ({
148
+ program: b,
149
+ resources: {
150
+ mask: t,
151
+ src: e,
152
+ dst: n,
153
+ out: r,
154
+ p: x(i, a)
155
+ },
156
+ workgroups: S
157
+ }), w = [C(g, h, d, 0, 0)];
158
+ for (let e of l) w.push(C(h, g, d, e, 1)), [h, g] = [g, h];
159
+ return w.push(C(h, g, d, 0, 2)), m && w.push(C(h, g, m, 0, 3)), e.submit(w), g.dispose(), {
160
+ nearest: h,
161
+ distance: d,
162
+ ids: m,
163
+ passes: l.length
164
+ };
165
+ }
166
+ function x(e) {
167
+ if (!(e instanceof n)) throw t("Seeds must be a GpuArray (or an expression) of a 2D or 3D mask", "compute");
168
+ let r = e.eval(), i = r.dtype === "u32" || r.dtype === "i32" ? r.ascontiguous() : r.ne(0).astype("u32").eval();
169
+ return r !== e && r !== i && r.dispose(), {
170
+ mask: i,
171
+ owned: i !== e
172
+ };
173
+ }
174
+ async function S(e, t = {}) {
175
+ let { mask: n, owned: r } = x(e);
176
+ try {
177
+ let { nearest: r, distance: i, passes: a } = await b(e.context, n, e.shape, t, !1, !1), o = () => {
178
+ r.dispose(), i.dispose();
179
+ };
180
+ return {
181
+ nearest: r,
182
+ distance: i,
183
+ passes: a,
184
+ dispose: o,
185
+ [Symbol.dispose]: o
186
+ };
187
+ } finally {
188
+ r && n.dispose();
189
+ }
190
+ }
191
+ async function C(e, t = {}) {
192
+ let n = await S(e, t);
193
+ return n.nearest.dispose(), n.distance;
194
+ }
195
+ async function w(e, t = {}) {
196
+ let { mask: n, owned: r } = x(e), i = e.context, a = (t.edge ?? .5) * (t.spacing ?? 1);
197
+ try {
198
+ let r = await b(i, n, e.shape, t, !1, !1);
199
+ r.nearest.dispose();
200
+ let o = await b(i, n, e.shape, t, !0, !1);
201
+ o.nearest.dispose();
202
+ let s = i.where(n.ne(0), o.distance.sub(a).mul(-1), r.distance.sub(a)).eval();
203
+ return r.distance.dispose(), o.distance.dispose(), s;
204
+ } finally {
205
+ r && n.dispose();
206
+ }
207
+ }
208
+ async function T(e, t, n, r = {}) {
209
+ let i = n.length;
210
+ _(n, r), t.length % i !== 0 && h(`Sites need ${i} coordinates each, got ${t.length} numbers`);
211
+ let a = new Uint32Array(n.reduce((e, t) => e * t, 1));
212
+ for (let e = t.length / i - 1; e >= 0; e--) {
213
+ let r = 0;
214
+ for (let a = i - 1; a >= 0; a--) {
215
+ let o = Math.round(t[e * i + a]);
216
+ o >= 0 && o < n[a] || h(`Site ${e} is outside the ${n.join(" x ")} grid`), r = r * n[a] + o;
217
+ }
218
+ a[r] = e + 1;
219
+ }
220
+ let o = e.array(a, {
221
+ shape: [...n],
222
+ dtype: "u32"
223
+ });
224
+ try {
225
+ let t = await b(e, o, n, r, !1, !0);
226
+ return t.nearest.dispose(), {
227
+ id: t.ids,
228
+ distance: t.distance,
229
+ passes: t.passes
230
+ };
231
+ } finally {
232
+ o.dispose();
233
+ }
234
+ }
235
+ function E(e, t, n = {}) {
236
+ let r = t.length;
237
+ r !== 2 && r !== 3 && h(`A distance field needs a 2D or 3D shape, got ${r} dimensions`);
238
+ let i = t.reduce((e, t) => e * t, 1);
239
+ (t.some((e) => !Number.isInteger(e) || e < 1) || e.length !== i) && h(`The mask has ${e.length} cells but shape [${t.join(", ")}] needs ${i}`);
240
+ let a = n.spacing ?? 1;
241
+ (!(a > 0) || !Number.isFinite(a)) && h(`Spacing must be a positive finite number, got ${a}`);
242
+ let o = 0x56bc75e2d63100000, s = (n) => {
243
+ let r = new Float64Array(i);
244
+ for (let t = 0; t < i; t++) r[t] = e[t] !== 0 === n ? o : 0;
245
+ return d(r, t);
246
+ }, c = (e) => e >= o / 2 ? p : Math.sqrt(e) * a, l = s(!1), u = new Float32Array(i);
247
+ if (!n.signed) {
248
+ for (let e = 0; e < i; e++) u[e] = c(l[e]);
249
+ return u;
250
+ }
251
+ let f = s(!0), m = (n.edge ?? .5) * a;
252
+ for (let t = 0; t < i; t++) u[t] = e[t] === 0 ? c(l[t]) - m : -(c(f[t]) - m);
253
+ return u;
254
+ }
255
+ //#endregion
256
+ export { p as NO_DISTANCE, f as NO_SEED, C as distanceTransform, E as distanceTransformExact, S as jumpFlood, g as jumpFloodSteps, w as signedDistance, T as voronoiMap };
@@ -0,0 +1,36 @@
1
+ /** A scalar the compute arrays hold. `f16` needs a device with the `shader-f16` feature. */
2
+ export type DType = 'f32' | 'f16' | 'i32' | 'u32';
3
+ /** The TypedArray that holds each element type on the CPU (`f16` as Float16Array where the runtime has it). */
4
+ export interface TypedArrayFor {
5
+ f32: Float32Array<ArrayBuffer>;
6
+ f16: Float16ArrayLike;
7
+ i32: Int32Array<ArrayBuffer>;
8
+ u32: Uint32Array<ArrayBuffer>;
9
+ }
10
+ /** A Float16Array (ES2025) where the runtime has one. */
11
+ export type Float16ArrayLike = ArrayLike<number> & ArrayBufferView<ArrayBuffer> & {
12
+ [index: number]: number;
13
+ };
14
+ /** Every element type. */
15
+ export declare const DTYPES: readonly DType[];
16
+ /** Bytes per element. */
17
+ export declare const bytesOf: (dtype: DType) => number;
18
+ /** True for the float types. */
19
+ export declare const isFloat: (dtype: DType) => boolean;
20
+ /** The CPU array constructor of `dtype`. Throws for `f16` on a runtime without Float16Array. */
21
+ export declare function arrayType(dtype: DType): new (data: ArrayLike<number> | ArrayBuffer) => ArrayLike<number>;
22
+ /** The element type a TypedArray maps to: floats to `f32` (Float16Array to `f16`), signed to `i32`, unsigned to `u32`. */
23
+ export declare function dtypeOf(data: ArrayLike<number> | ArrayBufferView): DType;
24
+ /** `data` as the CPU array of `dtype` (a copy only when the type differs). */
25
+ export declare function toTyped(data: ArrayLike<number>, dtype: DType): ArrayBufferView<ArrayBuffer>;
26
+ /**
27
+ * The result type of combining two types: any float wins (f32 over f16), i32 with u32 gives i32. A plain number
28
+ * adopts the array's type when it fits (an integer with an integer array), else f32, as NumPy 2 treats Python scalars.
29
+ */
30
+ export declare function promote(a: DType, b: DType): DType;
31
+ /** The type a JS number takes next to an array of `dtype`. */
32
+ export declare function scalarType(value: number, dtype: DType): DType;
33
+ /** The raw 32-bit pattern of `value` as `dtype`, for passing scalars through a u32 uniform. */
34
+ export declare function bitsOf(value: number, dtype: DType): number;
35
+ /** WGSL that reads a scalar of `dtype` from the u32 `bits`. */
36
+ export declare function fromBits(bits: string, dtype: DType): string;
@@ -0,0 +1,7 @@
1
+ //#region ../shared/errors.js
2
+ var e = (e, t) => Object.assign(Error(t), {
3
+ name: "MargoError",
4
+ code: e
5
+ }), t = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, n = (n, r, i = n) => e(n, t(r, i)), r = (e, n) => TypeError(t(e, n)), i = (e, n) => RangeError(t(e, n));
6
+ //#endregion
7
+ export { i as n, r, n as t };