wgblas 2.0.0 → 2.1.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 (66) hide show
  1. package/README.md +18 -18
  2. package/dist/wgblas.browser.js +1273 -1239
  3. package/index.d.mts +38 -6
  4. package/package.json +2 -1
  5. package/src/classes/GpuMatrix.mjs +17 -10
  6. package/src/classes/GpuVector.mjs +28 -10
  7. package/src/dasum/dasum.d.mts +4 -4
  8. package/src/dasum/dasum.mjs +19 -17
  9. package/src/devdocs.mjs +13 -0
  10. package/src/idamax/idamax.d.mts +20 -2
  11. package/src/idamax/idamax.mjs +18 -16
  12. package/src/init.mjs +114 -56
  13. package/src/isamax/isamax.d.mts +20 -2
  14. package/src/isamax/isamax.mjs +16 -14
  15. package/src/random/random.d.mts +1 -0
  16. package/src/sasum/sasum.d.mts +2 -2
  17. package/src/sasum/sasum.mjs +15 -13
  18. package/src/saxpy/saxpy.d.mts +2 -2
  19. package/src/saxpy/saxpy.mjs +10 -8
  20. package/src/scopy/scopy.d.mts +2 -2
  21. package/src/scopy/scopy.mjs +10 -8
  22. package/src/sdot/sdot.d.mts +2 -2
  23. package/src/sdot/sdot.mjs +16 -14
  24. package/src/sgemm/sgemm.mjs +28 -15
  25. package/src/sgemmtr/sgemmtr.mjs +16 -15
  26. package/src/sgemv/sgemv.mjs +38 -26
  27. package/src/sger/sger.mjs +10 -8
  28. package/src/shaders/index.mjs +164 -14
  29. package/src/shaders/reduction/scaledSum.wgsl +65 -0
  30. package/src/shaders/sgemm_large.wgsl +107 -18
  31. package/src/shaders/sgemm_small.wgsl +115 -15
  32. package/src/shaders/sgemmtr_large.wgsl +4 -1
  33. package/src/shaders/sgemmtr_small.wgsl +4 -1
  34. package/src/shaders/sgemv_n.wgsl +3 -1
  35. package/src/shaders/sgemv_t.wgsl +3 -1
  36. package/src/shaders/snrm2.wgsl +72 -23
  37. package/src/shaders/ssymv.wgsl +3 -1
  38. package/src/snrm2/snrm2.d.mts +2 -2
  39. package/src/snrm2/snrm2.mjs +33 -21
  40. package/src/srot/srot.d.mts +2 -4
  41. package/src/srot/srot.mjs +11 -9
  42. package/src/srotm/srotm.d.mts +2 -4
  43. package/src/srotm/srotm.mjs +19 -10
  44. package/src/sscal/sscal.d.mts +3 -3
  45. package/src/sscal/sscal.mjs +12 -10
  46. package/src/sswap/sswap.d.mts +2 -2
  47. package/src/sswap/sswap.mjs +11 -9
  48. package/src/ssymm/ssymm.mjs +31 -22
  49. package/src/ssymv/ssymv.mjs +10 -8
  50. package/src/ssyr/ssyr.mjs +9 -7
  51. package/src/ssyr2/ssyr2.mjs +10 -8
  52. package/src/ssyr2k/ssyr2k.mjs +18 -17
  53. package/src/ssyrk/ssyrk.mjs +18 -17
  54. package/src/strmm/strmm.mjs +47 -32
  55. package/src/strmv/strmv.mjs +10 -8
  56. package/src/strsm/strsm.mjs +54 -36
  57. package/src/strsv/strsv.mjs +16 -12
  58. package/src/util/benchmark.mjs +4 -6
  59. package/src/util/bindgroup.mjs +1 -3
  60. package/src/util/buffer.mjs +113 -19
  61. package/src/util/compute.mjs +6 -9
  62. package/src/util/constants.mjs +57 -0
  63. package/src/util/device.mjs +34 -0
  64. package/src/util/pipeline.mjs +5 -6
  65. package/src/util/workgroup.mjs +55 -7
  66. package/src/shaders/browser-shaders.mjs +0 -81
@@ -0,0 +1,57 @@
1
+ /** @module devdocs/utility-functions/constants */
2
+
3
+ /**
4
+ * Single source of truth for every constant the JS dispatch side shares with a
5
+ * shader.
6
+ *
7
+ * WGSL has no import mechanism, so each shader necessarily declares its own
8
+ * copy of these values. That makes them a silent-corruption hazard: change
9
+ * `BM` in `sgemm_large.wgsl` without changing `BM_LARGE` here and the host
10
+ * dispatches the wrong grid — too few workgroups computes part of the matrix
11
+ * and reports success. Hoisting them here removes the JS-to-JS duplication;
12
+ * `tests/utils/test.constants.js` closes the remaining JS-to-WGSL gap
13
+ * by parsing the shader sources and asserting they still agree.
14
+ *
15
+ * Every export below names the shader declaration it mirrors. Changing one
16
+ * means changing both, and the test will tell you if you forget.
17
+ */
18
+
19
+ // --- gemm block tiles ------------------------------------------------------
20
+
21
+ /** `BM` in sgemm_small.wgsl / sgemmtr_small.wgsl. */
22
+ export const BM_SMALL = 32;
23
+ /** `BN` in sgemm_small.wgsl / sgemmtr_small.wgsl. */
24
+ export const BN_SMALL = 32;
25
+ /** `BM` in sgemm_large.wgsl / sgemmtr_large.wgsl. */
26
+ export const BM_LARGE = 64;
27
+ /** `BN` in sgemm_large.wgsl / sgemmtr_large.wgsl. */
28
+ export const BN_LARGE = 64;
29
+
30
+ /**
31
+ * The large tile only pays for its bigger workgroups once the problem needs at
32
+ * least a 6x6 grid of them; below that the small tile wins. JS-only (no shader
33
+ * counterpart) — it selects *which* shader runs.
34
+ */
35
+ export const LARGE_TILE_WORKGROUP_THRESHOLD = 36;
36
+
37
+ // --- 1D / reduction kernels ------------------------------------------------
38
+
39
+ /**
40
+ * `const WGS: u32 = 64` — declared by every 1D and reduction shader, and the
41
+ * workgroup size `calcWorkgroups` divides by for a 1D dispatch.
42
+ */
43
+ export const WGS = 64;
44
+
45
+ // --- 2D helper kernels -----------------------------------------------------
46
+
47
+ /**
48
+ * `@workgroup_size(8, 8)` in symmetrize.wgsl, triangularize.wgsl and
49
+ * block_transfer.wgsl, and the size `calcWorkgroups` divides by per dimension
50
+ * for a 2D dispatch.
51
+ */
52
+ export const TILE_WG_2D = 8;
53
+
54
+ // --- triangular solve ------------------------------------------------------
55
+
56
+ /** `BLOCK_SIZE` in strsv_invert_block.wgsl — the diagonal block order. */
57
+ export const BLOCK_SIZE = 64;
@@ -0,0 +1,34 @@
1
+ /** @module devdocs/utility-functions/device */
2
+ import { GpuVector } from "../classes/GpuVector.mjs";
3
+ import { GpuMatrix } from "../classes/GpuMatrix.mjs";
4
+
5
+ /**
6
+ * Throws if any GPU-resident operand belongs to a device other than the one
7
+ * the routine was called with.
8
+ *
9
+ * A `GPUBuffer` is bound to the device that created it, and WebGPU has no way
10
+ * to share one across devices. Handing a routine a `GpuMatrix` from device A
11
+ * while passing device B fails deep inside bind-group creation as a
12
+ * `GPUValidationError` with no indication that two devices are involved —
13
+ * this turns it into a named, actionable error at the call boundary.
14
+ *
15
+ * Scalars, plain typed arrays and `undefined` entries are ignored, so callers
16
+ * can pass their whole operand set without filtering.
17
+ *
18
+ * @param {GPUDevice} device - the device the routine will dispatch on
19
+ * @param {string} routine - routine name, for the error message
20
+ * @param {Record<string, unknown>} operands - operand name -> value
21
+ * @throws {Error} if an operand is GPU-resident on a different device
22
+ */
23
+ export function requireSameDevice(device, routine, operands) {
24
+ for (const [name, value] of Object.entries(operands)) {
25
+ if (!(value instanceof GpuVector) && !(value instanceof GpuMatrix)) continue;
26
+ if (value.device !== device) {
27
+ throw new Error(
28
+ `${routine}: ${name} belongs to a different GPUDevice than the one passed in. ` +
29
+ "GPU buffers cannot be shared across devices — recreate the operand on this " +
30
+ "device, or call the routine with the device that owns it.",
31
+ );
32
+ }
33
+ }
34
+ }
@@ -1,5 +1,4 @@
1
1
  /** @module devdocs/utility-functions/pipeline */
2
- import { getDevice } from "../init.mjs";
3
2
 
4
3
  // WeakMap keyed by GPUDevice so pipelines are released automatically when the device is destroyed.
5
4
  const _pipelines = new WeakMap();
@@ -24,21 +23,21 @@ export async function getPipeline(device, shaderName, entryPoint = "main") {
24
23
  const names = Array.isArray(shaderName) ? shaderName : [shaderName];
25
24
  const key = `${names.join("+")}::${entryPoint}`;
26
25
  if (!byName.has(key)) {
27
- byName.set(key, await loadShader(names, entryPoint));
26
+ byName.set(key, await loadShader(device, names, entryPoint));
28
27
  }
29
28
  return byName.get(key);
30
29
  }
31
30
 
32
31
  /**
33
32
  * Loads WGSL source for `shaderName`. In the browser, reads from the inline bundle
34
- * (`browser-shaders.mjs`); in Node.js, reads the `.wgsl` file directly from disk.
33
+ * (`shaders/index.mjs`'s `shaderSources`); in Node.js, reads the `.wgsl` file directly from disk.
35
34
  * @param {string} shaderName
36
35
  * @returns {Promise<string>}
37
36
  */
38
37
  async function loadCode(shaderName) {
39
38
  // Check for Node.js explicitly — `window` is undefined in Web Workers too, so it's not a reliable signal.
40
39
  if (typeof process === "undefined" || !process.versions?.node) {
41
- const { shaderSources } = await import("../shaders/browser-shaders.mjs");
40
+ const { shaderSources } = await import("../shaders/index.mjs");
42
41
  const src = shaderSources[shaderName];
43
42
  if (!src) throw new Error(`Shader "${shaderName}" not found in browser bundle.`);
44
43
  return src;
@@ -56,6 +55,7 @@ async function loadCode(shaderName) {
56
55
  * `GPUComputePipeline`. Throws with line-level detail if compilation fails, rather than
57
56
  * surfacing a raw GPU error. Uses `layout: "auto"` so the pipeline derives its bind group
58
57
  * layout from the shader — no manual layout definition needed.
58
+ * @param {GPUDevice} device
59
59
  * @param {string[]} shaderNames - filenames without `.wgsl`, concatenated in array order
60
60
  * @param {string} [entryPoint="main"] - which `@compute` function in the combined module to run
61
61
  * @returns {Promise<GPUComputePipeline>}
@@ -64,8 +64,7 @@ async function loadCode(shaderName) {
64
64
  * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/GPUShaderModule/getCompilationInfo GPUShaderModule.getCompilationInfo()}
65
65
  * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/GPUDevice/createComputePipeline GPUDevice.createComputePipeline()}
66
66
  */
67
- export async function loadShader(shaderNames, entryPoint = "main") {
68
- const device = getDevice();
67
+ export async function loadShader(device, shaderNames, entryPoint = "main") {
69
68
  const label = shaderNames.join("+");
70
69
  const code = (await Promise.all(shaderNames.map(loadCode))).join("\n");
71
70
 
@@ -1,14 +1,20 @@
1
1
  /** @module devdocs/utility-functions/workgroup */
2
- import { getDevice } from "../init.mjs";
3
-
4
- // Fixed sizes match the shader declarations (WGS = 64 for 1D, 8×8 = 64 threads for 2D).
5
- const WORKGROUP_SIZE_1D = 64;
6
- const WORKGROUP_SIZE_2D = 8;
2
+ // Fixed sizes match the shader declarations (WGS = 64 for 1D, 8×8 = 64 threads
3
+ // for 2D) — see constants.mjs, which is where both values are defined and
4
+ // where the WGSL cross-check hangs off.
5
+ import { WGS as WORKGROUP_SIZE_1D, TILE_WG_2D as WORKGROUP_SIZE_2D } from "./constants.mjs";
7
6
 
8
7
  /**
9
8
  * Calculates the number of workgroups to dispatch, clamped to the device's
10
9
  * `maxComputeWorkgroupsPerDimension` limit (default 65535 across most devices).
11
10
  *
11
+ * ONLY for shaders whose kernel is a grid-stride loop driven by
12
+ * `num_workgroups` — those re-walk the whole domain regardless of how many
13
+ * workgroups actually launch, so clamping costs a little parallelism and
14
+ * nothing else. A shader that indexes straight off `workgroup_id` or
15
+ * `global_invocation_id` silently drops every row past the clamp; those must
16
+ * use {@link requireWorkgroups} / {@link requireWorkgroupCount} instead.
17
+ *
12
18
  * - 1D (pass only `rows`): returns a single count for `dispatchWorkgroups(n)`.
13
19
  * - 2D (pass both `rows` and `cols`): returns `{ x, y }` for `dispatchWorkgroups(x, y)`.
14
20
  * `rows` maps to the y dimension and `cols` maps to the x dimension.
@@ -18,8 +24,8 @@ const WORKGROUP_SIZE_2D = 8;
18
24
  * @returns {number | { x: number, y: number }}
19
25
  * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/GPUSupportedLimits GPUSupportedLimits} (`maxComputeWorkgroupsPerDimension`)
20
26
  */
21
- export function calcWorkgroups(rows, cols) {
22
- const max = getDevice().limits.maxComputeWorkgroupsPerDimension;
27
+ export function calcWorkgroups(device, rows, cols) {
28
+ const max = device.limits.maxComputeWorkgroupsPerDimension;
23
29
  if (cols === undefined) {
24
30
  return Math.min(Math.ceil(rows / WORKGROUP_SIZE_1D), max);
25
31
  } else {
@@ -29,3 +35,45 @@ export function calcWorkgroups(rows, cols) {
29
35
  };
30
36
  }
31
37
  }
38
+
39
+ /**
40
+ * Returns `count` unchanged if it fits the device's dispatch limit, and throws
41
+ * otherwise. The counterpart to {@link calcWorkgroups} for shaders with no
42
+ * grid-stride fallback: silently clamping those computes only part of the
43
+ * result and reports success, so a refusal is the safer failure.
44
+ *
45
+ * @param {number} count - workgroups this dispatch requires in one dimension
46
+ * @param {string} routine - routine name, for the error message
47
+ * @param {string} [dim] - dimension label ("x"/"y"), for the error message
48
+ * @returns {number} `count`
49
+ * @throws {Error} when `count` exceeds `maxComputeWorkgroupsPerDimension`
50
+ */
51
+ export function requireWorkgroupCount(device, count, routine, dim = "x") {
52
+ const max = device.limits.maxComputeWorkgroupsPerDimension;
53
+ if (count > max)
54
+ throw new Error(
55
+ `${routine}: this problem needs ${count} workgroups in ${dim}, but the device allows ` +
56
+ `${max} (maxComputeWorkgroupsPerDimension). The operands are too large for this device — ` +
57
+ `split the operation into smaller blocks.`,
58
+ );
59
+ return count;
60
+ }
61
+
62
+ /**
63
+ * {@link calcWorkgroups} with the clamp replaced by a throw — same arguments
64
+ * and same return shape, for shaders without a grid-stride fallback.
65
+ *
66
+ * @param {string} routine - routine name, for the error message
67
+ * @param {number} rows - row count (1D: element count)
68
+ * @param {number} [cols] - column count; omit for a 1D dispatch
69
+ * @returns {number | { x: number, y: number }}
70
+ * @throws {Error} when either dimension exceeds `maxComputeWorkgroupsPerDimension`
71
+ */
72
+ export function requireWorkgroups(device, routine, rows, cols) {
73
+ if (cols === undefined)
74
+ return requireWorkgroupCount(device, Math.ceil(rows / WORKGROUP_SIZE_1D), routine);
75
+ return {
76
+ x: requireWorkgroupCount(device, Math.ceil(cols / WORKGROUP_SIZE_2D), routine, "x"),
77
+ y: requireWorkgroupCount(device, Math.ceil(rows / WORKGROUP_SIZE_2D), routine, "y"),
78
+ };
79
+ }
@@ -1,81 +0,0 @@
1
- import argmax from "./reduction/argmax.wgsl";
2
- import argmaxF64 from "./reduction/argmaxF64.wgsl";
3
- import sum from "./reduction/sum.wgsl";
4
- import sumF64 from "./reduction/sumF64.wgsl";
5
- import sscal from "./sscal.wgsl";
6
- import sswap from "./sswap.wgsl";
7
- import saxpy from "./saxpy.wgsl";
8
- import scopy from "./scopy.wgsl";
9
- import sdot from "./sdot.wgsl";
10
- import sasum from "./sasum.wgsl";
11
- import snrm2 from "./snrm2.wgsl";
12
- import srot from "./srot.wgsl";
13
- import srotm from "./srotm.wgsl";
14
- import isamax from "./isamax.wgsl";
15
- import sgemv_n from "./sgemv_n.wgsl";
16
- import sgemv_t from "./sgemv_t.wgsl";
17
- import ssymv from "./ssymv.wgsl";
18
- import strmv from "./strmv.wgsl";
19
- import sger from "./sger.wgsl";
20
- import ssyr from "./ssyr.wgsl";
21
- import ssyr2 from "./ssyr2.wgsl";
22
- import f64add from "./f64add.wgsl";
23
- import dekker from "./f64/dekker.wgsl";
24
- import ddAbs from "./f64/utils/abs.wgsl";
25
- import ddAddUtil from "./f64/utils/add.wgsl";
26
- import ddGreater from "./f64/utils/greater.wgsl";
27
- import ddEqual from "./f64/utils/equal.wgsl";
28
- import dasum from "./dasum.wgsl";
29
- import idamax from "./idamax.wgsl";
30
- import strsv_invert_block from "./strsv_invert_block.wgsl";
31
- import strsv_apply_inverse from "./strsv_apply_inverse.wgsl";
32
- import strsv_update from "./strsv_update.wgsl";
33
- import sgemm_small from "./sgemm_small.wgsl";
34
- import sgemm_large from "./sgemm_large.wgsl";
35
- import sgemmtr_small from "./sgemmtr_small.wgsl";
36
- import sgemmtr_large from "./sgemmtr_large.wgsl";
37
- import symmetrize from "./symmetrize.wgsl";
38
- import triangularize from "./triangularize.wgsl";
39
- import blockTransfer from "./block_transfer.wgsl";
40
-
41
- export const shaderSources = {
42
- "reduction/argmax": argmax,
43
- "reduction/argmaxF64": argmaxF64,
44
- "reduction/sum": sum,
45
- "reduction/sumF64": sumF64,
46
- sscal,
47
- sswap,
48
- saxpy,
49
- scopy,
50
- sdot,
51
- sasum,
52
- snrm2,
53
- srot,
54
- srotm,
55
- isamax,
56
- sgemv_n,
57
- sgemv_t,
58
- ssymv,
59
- strmv,
60
- sger,
61
- ssyr,
62
- ssyr2,
63
- f64add,
64
- "f64/dekker": dekker,
65
- "f64/utils/abs": ddAbs,
66
- "f64/utils/add": ddAddUtil,
67
- "f64/utils/greater": ddGreater,
68
- "f64/utils/equal": ddEqual,
69
- dasum,
70
- idamax,
71
- strsv_invert_block,
72
- strsv_apply_inverse,
73
- strsv_update,
74
- sgemm_small,
75
- sgemm_large,
76
- sgemmtr_small,
77
- sgemmtr_large,
78
- symmetrize,
79
- triangularize,
80
- block_transfer: blockTransfer,
81
- };