wgblas 1.2.1 → 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 (96) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +54 -72
  3. package/dist/wgblas.browser.js +1637 -858
  4. package/index.d.mts +51 -6
  5. package/index.mjs +8 -0
  6. package/package.json +56 -2
  7. package/src/classes/GpuMatrix.mjs +17 -10
  8. package/src/classes/GpuVector.mjs +28 -10
  9. package/src/dasum/dasum.d.mts +6 -6
  10. package/src/dasum/dasum.mjs +25 -21
  11. package/src/devdocs.mjs +13 -0
  12. package/src/idamax/idamax.d.mts +69 -0
  13. package/src/idamax/idamax.mjs +130 -0
  14. package/src/init.mjs +115 -49
  15. package/src/isamax/isamax.d.mts +21 -3
  16. package/src/isamax/isamax.mjs +16 -14
  17. package/src/random/random.d.mts +1 -0
  18. package/src/sasum/sasum.d.mts +3 -3
  19. package/src/sasum/sasum.mjs +15 -13
  20. package/src/saxpy/saxpy.d.mts +3 -3
  21. package/src/saxpy/saxpy.mjs +10 -8
  22. package/src/scopy/scopy.d.mts +3 -3
  23. package/src/scopy/scopy.mjs +10 -8
  24. package/src/sdot/sdot.d.mts +3 -3
  25. package/src/sdot/sdot.mjs +16 -14
  26. package/src/sgemm/sgemm.d.mts +102 -0
  27. package/src/sgemm/sgemm.mjs +208 -0
  28. package/src/sgemmtr/sgemmtr.d.mts +104 -0
  29. package/src/sgemmtr/sgemmtr.mjs +204 -0
  30. package/src/sgemv/sgemv.d.mts +1 -38
  31. package/src/sgemv/sgemv.mjs +42 -26
  32. package/src/sger/sger.d.mts +1 -34
  33. package/src/sger/sger.mjs +12 -8
  34. package/src/shaders/block_transfer.wgsl +42 -0
  35. package/src/shaders/dasum.wgsl +3 -2
  36. package/src/shaders/f64/dekker.wgsl +4 -85
  37. package/src/shaders/f64/utils/abs.wgsl +10 -0
  38. package/src/shaders/f64/utils/add.wgsl +77 -0
  39. package/src/shaders/f64/utils/equal.wgsl +7 -0
  40. package/src/shaders/f64/utils/greater.wgsl +12 -0
  41. package/src/shaders/f64/utils/multiply.wgsl +81 -0
  42. package/src/shaders/idamax.wgsl +96 -0
  43. package/src/shaders/index.mjs +164 -14
  44. package/src/shaders/reduction/argmaxF64.wgsl +50 -0
  45. package/src/shaders/reduction/scaledSum.wgsl +65 -0
  46. package/src/shaders/reduction/sumF64.wgsl +2 -2
  47. package/src/shaders/sgemm_large.wgsl +206 -0
  48. package/src/shaders/sgemm_small.wgsl +212 -0
  49. package/src/shaders/sgemmtr_large.wgsl +120 -0
  50. package/src/shaders/sgemmtr_small.wgsl +113 -0
  51. package/src/shaders/sgemv_n.wgsl +3 -1
  52. package/src/shaders/sgemv_t.wgsl +3 -1
  53. package/src/shaders/snrm2.wgsl +72 -23
  54. package/src/shaders/ssymv.wgsl +3 -1
  55. package/src/shaders/symmetrize.wgsl +31 -0
  56. package/src/shaders/triangularize.wgsl +44 -0
  57. package/src/snrm2/snrm2.d.mts +3 -3
  58. package/src/snrm2/snrm2.mjs +33 -21
  59. package/src/srot/srot.d.mts +3 -5
  60. package/src/srot/srot.mjs +11 -9
  61. package/src/srotm/srotm.d.mts +3 -5
  62. package/src/srotm/srotm.mjs +19 -10
  63. package/src/sscal/sscal.d.mts +4 -4
  64. package/src/sscal/sscal.mjs +12 -10
  65. package/src/sswap/sswap.d.mts +3 -3
  66. package/src/sswap/sswap.mjs +11 -9
  67. package/src/ssymm/ssymm.d.mts +103 -0
  68. package/src/ssymm/ssymm.mjs +218 -0
  69. package/src/ssymv/ssymv.d.mts +1 -36
  70. package/src/ssymv/ssymv.mjs +12 -8
  71. package/src/ssyr/ssyr.d.mts +1 -30
  72. package/src/ssyr/ssyr.mjs +11 -7
  73. package/src/ssyr2/ssyr2.d.mts +1 -34
  74. package/src/ssyr2/ssyr2.mjs +12 -8
  75. package/src/ssyr2k/ssyr2k.d.mts +100 -0
  76. package/src/ssyr2k/ssyr2k.mjs +202 -0
  77. package/src/ssyrk/ssyrk.d.mts +90 -0
  78. package/src/ssyrk/ssyrk.mjs +177 -0
  79. package/src/strmm/strmm.d.mts +100 -0
  80. package/src/strmm/strmm.mjs +226 -0
  81. package/src/strmv/strmv.d.mts +1 -36
  82. package/src/strmv/strmv.mjs +12 -8
  83. package/src/strsm/strsm.d.mts +99 -0
  84. package/src/strsm/strsm.mjs +360 -0
  85. package/src/strsv/strsv.d.mts +1 -32
  86. package/src/strsv/strsv.mjs +18 -12
  87. package/src/util/benchmark.mjs +4 -6
  88. package/src/util/bindgroup.mjs +1 -3
  89. package/src/util/buffer.mjs +116 -20
  90. package/src/util/compute.mjs +12 -12
  91. package/src/util/constants.mjs +57 -0
  92. package/src/util/device.mjs +34 -0
  93. package/src/util/f64.mjs +3 -3
  94. package/src/util/pipeline.mjs +5 -6
  95. package/src/util/workgroup.mjs +55 -7
  96. package/src/shaders/browser-shaders.mjs +0 -55
@@ -11,6 +11,7 @@ import { extractTimestamp } from "../util/benchmark.mjs";
11
11
  import { getPipeline } from "../util/pipeline.mjs";
12
12
  import { GpuVector } from "../classes/GpuVector.mjs";
13
13
  import { GpuMatrix } from "../classes/GpuMatrix.mjs";
14
+ import { requireSameDevice } from "../util/device.mjs";
14
15
 
15
16
  export async function ssyr2(device, uplo, n, alpha, x, incx, y, incy, A, lda, layout = "row-major") {
16
17
  const xIsGpu = x instanceof GpuVector;
@@ -19,6 +20,7 @@ export async function ssyr2(device, uplo, n, alpha, x, incx, y, incy, A, lda, la
19
20
 
20
21
  if (!(device instanceof GPUDevice))
21
22
  throw new Error("device must be a GPUDevice.");
23
+ requireSameDevice(device, "ssyr2", { A, x, y });
22
24
  if (uplo !== "lower" && uplo !== "upper")
23
25
  throw new Error("uplo must be 'lower' or 'upper'.");
24
26
  if (layout !== "row-major" && layout !== "column-major")
@@ -49,6 +51,8 @@ export async function ssyr2(device, uplo, n, alpha, x, incx, y, incy, A, lda, la
49
51
  );
50
52
  if (xIsGpu && !AIsGpu)
51
53
  throw new Error("A must be a GpuMatrix when x and y are GpuVectors.");
54
+ if (AIsGpu && !xIsGpu)
55
+ throw new Error("x and y must be GpuVectors when A is a GpuMatrix.");
52
56
  if (AIsGpu && xIsGpu && A._buf === x._buf)
53
57
  throw new Error("A and x must not reference the same GPU buffer.");
54
58
  if (AIsGpu && yIsGpu && A._buf === y._buf)
@@ -81,10 +85,10 @@ export async function ssyr2(device, uplo, n, alpha, x, incx, y, incy, A, lda, la
81
85
  let paramsBuffer = null;
82
86
 
83
87
  try {
84
- xBuffer = xIsGpu ? x._buf : uploadBuffer(x, "ssyr2-x", false);
85
- yBuffer = yIsGpu ? y._buf : uploadBuffer(y, "ssyr2-y", false);
86
- ABuffer = AIsGpu ? A._buf : uploadBuffer(A, "ssyr2-A", true);
87
- paramsBuffer = createParamsBuffer(
88
+ xBuffer = xIsGpu ? x._buf : uploadBuffer(device, x, "ssyr2-x", false);
89
+ yBuffer = yIsGpu ? y._buf : uploadBuffer(device, y, "ssyr2-y", false);
90
+ ABuffer = AIsGpu ? A._buf : uploadBuffer(device, A, "ssyr2-A", true);
91
+ paramsBuffer = createParamsBuffer(device,
88
92
  [
89
93
  { value: n, type: "u32" },
90
94
  { value: alpha, type: "f32" },
@@ -96,7 +100,7 @@ export async function ssyr2(device, uplo, n, alpha, x, incx, y, incy, A, lda, la
96
100
  "ssyr2-params",
97
101
  );
98
102
 
99
- const bindGroup = createBindGroup(pipeline.getBindGroupLayout(0), [
103
+ const bindGroup = createBindGroup(device, pipeline.getBindGroupLayout(0), [
100
104
  xBuffer,
101
105
  yBuffer,
102
106
  ABuffer,
@@ -106,10 +110,10 @@ export async function ssyr2(device, uplo, n, alpha, x, incx, y, incy, A, lda, la
106
110
  // One workgroup per row of A; clamped to device limit — the shader's
107
111
  // grid-stride loop handles remaining rows when n > dispatch count.
108
112
  const wgCount = Math.min(n, device.limits.maxComputeWorkgroupsPerDimension);
109
- const { commandEncoder, ts } = runComputePass(pipeline, bindGroup, wgCount);
110
- const readBuffer = AIsGpu ? null : stageReadback(commandEncoder, ABuffer);
113
+ const { commandEncoder, ts } = runComputePass(device, pipeline, bindGroup, wgCount);
114
+ const readBuffer = AIsGpu ? null : stageReadback(device, commandEncoder, ABuffer);
111
115
 
112
- submit(commandEncoder);
116
+ submit(device, commandEncoder);
113
117
 
114
118
  const gpuTimeMs = await extractTimestamp(ts);
115
119
 
@@ -0,0 +1,100 @@
1
+ import { GpuMatrix } from "../classes/GpuMatrix.mjs";
2
+
3
+ /**
4
+ * Performs the symmetric rank-2k update
5
+ * C := uplo(alpha * op(A) * op(B)^T + alpha * op(B) * op(A)^T + beta * C) —
6
+ * only the triangle of C named by `uplo` is read or written (`'lower'`:
7
+ * `col <= row`, `'upper'`: `col >= row`). C is always n×n.
8
+ *
9
+ * - `trans='no-transpose'`: op(A) = A, op(B) = B (both n×k)
10
+ * - `trans='transpose'`: op(A) = A^T, op(B) = B^T (A, B stored k×n)
11
+ *
12
+ * No dedicated kernel — two passes of `sgemmtr`'s kernel
13
+ * (`sgemmtr_small.wgsl`/`sgemmtr_large.wgsl`) on one command encoder, the
14
+ * second accumulating with `beta=1` so C never leaves the GPU between them.
15
+ *
16
+ * {@includeCode ../../examples/ssyr2k/ssyr2k.js}
17
+ *
18
+ * **Browser (standalone HTML):**
19
+ * {@includeCode ../../examples/ssyr2k/web/ssyr2k.html}
20
+ *
21
+ * @param device - GPUDevice from `init()`
22
+ * @param uplo - `'lower'` to update only `col <= row`, `'upper'` for `col >= row`
23
+ * @param trans - `'no-transpose'` for A*B^T + B*A^T, `'transpose'` for A^T*B + B^T*A
24
+ * @param n - order of C (C is n×n); rows of op(A) and op(B)
25
+ * @param k - columns of op(A) and op(B) — the shared/contracted dimension
26
+ * @param alpha - scalar multiplier for both rank-k terms
27
+ * @param A - Float32Array, row-major or column-major (see `layout`)
28
+ * @param lda - leading dimension of A as stored
29
+ * @param B - Float32Array, row-major or column-major (see `layout`)
30
+ * @param ldb - leading dimension of B as stored
31
+ * @param beta - scalar multiplier for C
32
+ * @param C - Float32Array input/output matrix, row-major or column-major
33
+ * @param ldc - leading dimension of C as stored
34
+ * @param layout - storage layout shared by A/B/C when they're Float32Array
35
+ * (default: `'row-major'`) — same handling as `sgemmtr`; column-major C
36
+ * flips `uplo` internally so it still names the triangle you asked for
37
+ * @returns updated C as a Float32Array (only the requested triangle changed)
38
+ * @see <a href="https://github.com/manit2004/wgblas/blob/main/src/ssyr2k/ssyr2k.mjs#L20">Source code: ssyr2k.mjs (L20)</a>
39
+ * @category BLAS Level 3
40
+ */
41
+ export declare function ssyr2k(
42
+ device: GPUDevice,
43
+ uplo: 'lower' | 'upper',
44
+ trans: 'no-transpose' | 'transpose',
45
+ n: number,
46
+ k: number,
47
+ alpha: number,
48
+ A: Float32Array,
49
+ lda: number,
50
+ B: Float32Array,
51
+ ldb: number,
52
+ beta: number,
53
+ C: Float32Array,
54
+ ldc: number,
55
+ layout?: 'row-major' | 'column-major',
56
+ ): Promise<{ C: Float32Array; gpuTimeMs?: number }>;
57
+
58
+ /**
59
+ * Performs the symmetric rank-2k update
60
+ * C := uplo(alpha * op(A) * op(B)^T + alpha * op(B) * op(A)^T + beta * C)
61
+ *
62
+ * A, B, and C are all kept GPU-resident. Each matrix's own `layout` (set at
63
+ * `GpuMatrix.from` time) determines the operation — there is no separate
64
+ * `layout` argument here. A and B must be GpuMatrix whenever C is, and vice
65
+ * versa — mixing a GpuMatrix with a plain Float32Array is not supported.
66
+ *
67
+ * {@includeCode ../../examples/ssyr2k/gpu.ssyr2k.js}
68
+ *
69
+ * @param device - GPUDevice from `init()`
70
+ * @param uplo - `'lower'` to update only `col <= row`, `'upper'` for `col >= row`
71
+ * @param trans - `'no-transpose'` for A*B^T + B*A^T, `'transpose'` for A^T*B + B^T*A
72
+ * @param n - order of C (C is n×n); rows of op(A) and op(B)
73
+ * @param k - columns of op(A) and op(B) — the shared/contracted dimension
74
+ * @param alpha - scalar multiplier for both rank-k terms
75
+ * @param A - GpuMatrix
76
+ * @param lda - leading dimension of A (must equal A.lda)
77
+ * @param B - GpuMatrix
78
+ * @param ldb - leading dimension of B (must equal B.lda)
79
+ * @param beta - scalar multiplier for C
80
+ * @param C - GpuMatrix (mutated in place; only the requested triangle changes)
81
+ * @param ldc - leading dimension of C (must equal C.lda)
82
+ * @returns no C — it stays GPU-resident; call `C.read()` yourself for a CPU readback (see the example)
83
+ * @see <a href="https://github.com/manit2004/wgblas/blob/main/src/ssyr2k/ssyr2k.mjs#L20">Source code: ssyr2k.mjs (L20)</a>
84
+ * @category BLAS Level 3
85
+ */
86
+ export declare function ssyr2k(
87
+ device: GPUDevice,
88
+ uplo: 'lower' | 'upper',
89
+ trans: 'no-transpose' | 'transpose',
90
+ n: number,
91
+ k: number,
92
+ alpha: number,
93
+ A: GpuMatrix,
94
+ lda: number,
95
+ B: GpuMatrix,
96
+ ldb: number,
97
+ beta: number,
98
+ C: GpuMatrix,
99
+ ldc: number,
100
+ ): Promise<{ gpuTimeMs?: number }>;
@@ -0,0 +1,202 @@
1
+ import {
2
+ uploadBuffer,
3
+ createParamsBuffer,
4
+ stageReadback,
5
+ destroyBuffers,
6
+ } from "../util/buffer.mjs";
7
+ import { createBindGroup } from "../util/bindgroup.mjs";
8
+ import { beginTimedEncoder, encodePass, submit } from "../util/compute.mjs";
9
+ import { extractResult } from "../util/result.mjs";
10
+ import { resolveTimestamp, extractTimestamp } from "../util/benchmark.mjs";
11
+ import { getPipeline } from "../util/pipeline.mjs";
12
+ import { GpuMatrix } from "../classes/GpuMatrix.mjs";
13
+ import { requireWorkgroupCount } from "../util/workgroup.mjs";
14
+ import { BM_SMALL, BN_SMALL, BM_LARGE, BN_LARGE, LARGE_TILE_WORKGROUP_THRESHOLD } from "../util/constants.mjs";
15
+ import { requireSameDevice } from "../util/device.mjs";
16
+
17
+
18
+ // ssyr2k: C := uplo(alpha*op(A)*op(B)^T + alpha*op(B)*op(A)^T + beta*C). No
19
+ // dedicated shader — two sgemmtr passes on one encoder, second with beta=1.
20
+ export async function ssyr2k(
21
+ device, uplo, trans, n, k, alpha, A, lda, B, ldb, beta, C, ldc, layout = "row-major",
22
+ ) {
23
+ const AIsGpu = A instanceof GpuMatrix;
24
+ const BIsGpu = B instanceof GpuMatrix;
25
+ const CIsGpu = C instanceof GpuMatrix;
26
+
27
+ if (!(device instanceof GPUDevice))
28
+ throw new Error("device must be a GPUDevice.");
29
+ requireSameDevice(device, "ssyr2k", { A, B, C });
30
+ if (uplo !== "lower" && uplo !== "upper")
31
+ throw new Error("uplo must be 'lower' or 'upper'.");
32
+ if (trans !== "no-transpose" && trans !== "transpose")
33
+ throw new Error("trans must be 'no-transpose' or 'transpose'.");
34
+ if (layout !== "row-major" && layout !== "column-major")
35
+ throw new Error("layout must be 'row-major' or 'column-major'.");
36
+ if (typeof alpha !== "number")
37
+ throw new Error("alpha must be a number.");
38
+ if (Number.isNaN(alpha)) throw new Error("alpha must not be NaN.");
39
+ if (!Number.isFinite(alpha)) throw new Error("alpha must be finite.");
40
+ if (typeof beta !== "number")
41
+ throw new Error("beta must be a number.");
42
+ if (Number.isNaN(beta)) throw new Error("beta must not be NaN.");
43
+ if (!Number.isFinite(beta)) throw new Error("beta must be finite.");
44
+ if (
45
+ !Number.isInteger(n) || !Number.isInteger(k) ||
46
+ !Number.isInteger(lda) || !Number.isInteger(ldb) || !Number.isInteger(ldc)
47
+ )
48
+ throw new Error("n, k, lda, ldb, and ldc must be integers.");
49
+ if (!AIsGpu && !(A instanceof Float32Array))
50
+ throw new Error("A must be a Float32Array or GpuMatrix.");
51
+ if (!BIsGpu && !(B instanceof Float32Array))
52
+ throw new Error("B must be a Float32Array or GpuMatrix.");
53
+ if (!CIsGpu && !(C instanceof Float32Array))
54
+ throw new Error("C must be a Float32Array or GpuMatrix.");
55
+ if ((AIsGpu || BIsGpu) && !CIsGpu)
56
+ throw new Error("C must be a GpuMatrix when A or B is a GpuMatrix.");
57
+ if (CIsGpu && (!AIsGpu || !BIsGpu))
58
+ throw new Error("A and B must be GpuMatrix when C is a GpuMatrix.");
59
+ if (n < 0 || k < 0) throw new Error("n and k must be non-negative.");
60
+ if (n === 0) return CIsGpu ? {} : { C };
61
+
62
+ const effLayoutA = AIsGpu ? A.layout : layout;
63
+ const effLayoutB = BIsGpu ? B.layout : layout;
64
+ const effLayoutC = CIsGpu ? C.layout : layout;
65
+
66
+ // A: op(A) is n x k; A itself is n x k or k x n depending on trans.
67
+ const aRows = effLayoutA === "column-major" ? k : n;
68
+ const aCols = effLayoutA === "column-major" ? n : k;
69
+ const aOuter = trans === "no-transpose" ? aRows : aCols;
70
+ const aInner = trans === "no-transpose" ? aCols : aRows;
71
+ if (lda < aInner)
72
+ throw new Error(`lda must be >= ${effLayoutA === "column-major" ? "rows" : "cols"} of A as stored.`);
73
+ if (AIsGpu) {
74
+ if (lda !== A.lda) throw new Error("lda must match A.lda when A is a GpuMatrix.");
75
+ const [aLogRows, aLogCols] = trans === "no-transpose" ? [n, k] : [k, n];
76
+ if (A.rows < aLogRows || A.cols < aLogCols)
77
+ throw new Error("A is too small for the given n, k, and trans.");
78
+ } else if (A.length < (aOuter - 1) * lda + aInner) {
79
+ throw new Error("A does not have enough elements for the given dimensions and lda.");
80
+ }
81
+
82
+ // B: same shape rule as A — netlib's syr2k shares one trans across both operands.
83
+ const bRows = effLayoutB === "column-major" ? k : n;
84
+ const bCols = effLayoutB === "column-major" ? n : k;
85
+ const bOuter = trans === "no-transpose" ? bRows : bCols;
86
+ const bInner = trans === "no-transpose" ? bCols : bRows;
87
+ if (ldb < bInner)
88
+ throw new Error(`ldb must be >= ${effLayoutB === "column-major" ? "rows" : "cols"} of B as stored.`);
89
+ if (BIsGpu) {
90
+ if (ldb !== B.lda) throw new Error("ldb must match B.lda when B is a GpuMatrix.");
91
+ const [bLogRows, bLogCols] = trans === "no-transpose" ? [n, k] : [k, n];
92
+ if (B.rows < bLogRows || B.cols < bLogCols)
93
+ throw new Error("B is too small for the given n, k, and trans.");
94
+ } else if (B.length < (bOuter - 1) * ldb + bInner) {
95
+ throw new Error("B does not have enough elements for the given dimensions and ldb.");
96
+ }
97
+
98
+ // C: always n x n symmetric — layout only affects storage order, not size.
99
+ if (ldc < n) throw new Error("ldc must be >= n.");
100
+ if (CIsGpu) {
101
+ if (ldc !== C.lda) throw new Error("ldc must match C.lda when C is a GpuMatrix.");
102
+ if (C.rows < n || C.cols < n) throw new Error("C is too small for the given n.");
103
+ } else if (C.length < (n - 1) * ldc + n) {
104
+ throw new Error("C does not have enough elements for the given dimensions and ldc.");
105
+ }
106
+
107
+ // Column-major A/B reinterpreted row-major is A^T/B^T — flip trans per operand.
108
+ let effTransA = trans;
109
+ if (effLayoutA === "column-major")
110
+ effTransA = effTransA === "no-transpose" ? "transpose" : "no-transpose";
111
+ let effTransB = trans;
112
+ if (effLayoutB === "column-major")
113
+ effTransB = effTransB === "no-transpose" ? "transpose" : "no-transpose";
114
+
115
+ const uploEff = effLayoutC === "column-major"
116
+ ? (uplo === "lower" ? "upper" : "lower")
117
+ : uplo;
118
+ const flip = (t) => (t === "no-transpose" ? "transpose" : "no-transpose");
119
+
120
+ // One pass: op(X) as-is, op(Y) transposed. transOwnY is explicit, not inferred.
121
+ function passShape(transOwnX, X, ldX, transOwnY, Y, ldY) {
122
+ const transX = transOwnX;
123
+ const transY = flip(transOwnY);
124
+ if (effLayoutC !== "column-major") return { transX, X, ldX, transY, Y, ldY };
125
+ return { transX: flip(transY), X: Y, ldX: ldY, transY: flip(transX), Y: X, ldY: ldX };
126
+ }
127
+
128
+ // Shape-based auto-select — see sgemmtr_small.wgsl/sgemmtr_large.wgsl. m=n=n here (square C).
129
+ const largeWgX = Math.ceil(n / BN_LARGE);
130
+ const largeWgY = Math.ceil(n / BM_LARGE);
131
+ const useLargeTile = largeWgX * largeWgY >= LARGE_TILE_WORKGROUP_THRESHOLD;
132
+ const pipeline = await getPipeline(device, useLargeTile ? "sgemmtr_large" : "sgemmtr_small");
133
+ const wgCount = useLargeTile
134
+ ? {
135
+ x: requireWorkgroupCount(device, largeWgX, "ssyr2k", "x"),
136
+ y: requireWorkgroupCount(device, largeWgY, "ssyr2k", "y"),
137
+ }
138
+ : {
139
+ x: requireWorkgroupCount(device, Math.ceil(n / BN_SMALL), "ssyr2k", "x"),
140
+ y: requireWorkgroupCount(device, Math.ceil(n / BM_SMALL), "ssyr2k", "y"),
141
+ };
142
+
143
+ const ABuffer = AIsGpu ? A._buf : uploadBuffer(device, A, "ssyr2k-A", false);
144
+ const BBuffer = BIsGpu ? B._buf : uploadBuffer(device, B, "ssyr2k-B", false);
145
+ const CBuffer = CIsGpu ? C._buf : uploadBuffer(device, C, "ssyr2k-C", true);
146
+ let paramsBuffer1 = null, paramsBuffer2 = null;
147
+
148
+ try {
149
+ const pass1 = passShape(effTransA, ABuffer, lda, effTransB, BBuffer, ldb);
150
+ const pass2 = passShape(effTransB, BBuffer, ldb, effTransA, ABuffer, lda);
151
+
152
+ const makeParams = (p, betaVal) => createParamsBuffer(device,
153
+ [
154
+ { value: n, type: "u32" },
155
+ { value: n, type: "u32" },
156
+ { value: k, type: "u32" },
157
+ { value: alpha, type: "f32" },
158
+ { value: betaVal, type: "f32" },
159
+ { value: p.ldX, type: "u32" },
160
+ { value: p.ldY, type: "u32" },
161
+ { value: ldc, type: "u32" },
162
+ { value: p.transX === "transpose" ? 1 : 0, type: "u32" },
163
+ { value: p.transY === "transpose" ? 1 : 0, type: "u32" },
164
+ { value: uploEff === "upper" ? 1 : 0, type: "u32" },
165
+ ],
166
+ "ssyr2k-params",
167
+ );
168
+ paramsBuffer1 = makeParams(pass1, beta);
169
+ paramsBuffer2 = makeParams(pass2, 1.0);
170
+
171
+ const bindGroup1 = createBindGroup(device, pipeline.getBindGroupLayout(0), [pass1.X, pass1.Y, CBuffer, paramsBuffer1]);
172
+ const bindGroup2 = createBindGroup(device, pipeline.getBindGroupLayout(0), [pass2.X, pass2.Y, CBuffer, paramsBuffer2]);
173
+
174
+ const { commandEncoder, querySet } = beginTimedEncoder(device);
175
+ const desc1 = querySet ? { timestampWrites: { querySet, beginningOfPassWriteIndex: 0 } } : undefined;
176
+ const desc2 = querySet ? { timestampWrites: { querySet, endOfPassWriteIndex: 1 } } : undefined;
177
+ encodePass(commandEncoder, pipeline, bindGroup1, wgCount, desc1);
178
+ encodePass(commandEncoder, pipeline, bindGroup2, wgCount, desc2);
179
+
180
+ const ts = resolveTimestamp(device, commandEncoder, querySet);
181
+ const readBuffer = CIsGpu ? null : stageReadback(device, commandEncoder, CBuffer);
182
+
183
+ submit(device, commandEncoder);
184
+
185
+ const gpuTimeMs = await extractTimestamp(ts);
186
+
187
+ if (CIsGpu) {
188
+ if (gpuTimeMs !== undefined) return { gpuTimeMs };
189
+ return {};
190
+ }
191
+
192
+ const result = await extractResult(readBuffer, Float32Array);
193
+ if (gpuTimeMs !== undefined) return { C: result, gpuTimeMs };
194
+ return { C: result };
195
+ } finally {
196
+ if (!AIsGpu) destroyBuffers(ABuffer);
197
+ if (!BIsGpu) destroyBuffers(BBuffer);
198
+ if (!CIsGpu) destroyBuffers(CBuffer);
199
+ if (paramsBuffer1) destroyBuffers(paramsBuffer1);
200
+ if (paramsBuffer2) destroyBuffers(paramsBuffer2);
201
+ }
202
+ }
@@ -0,0 +1,90 @@
1
+ import { GpuMatrix } from "../classes/GpuMatrix.mjs";
2
+
3
+ /**
4
+ * Performs the symmetric rank-k update C := uplo(alpha * op(A) * op(A)^T + beta * C) —
5
+ * only the triangle of C named by `uplo` is read or written (`'lower'`: `col <= row`,
6
+ * `'upper'`: `col >= row`). C is always n×n.
7
+ *
8
+ * - `trans='no-transpose'`: op(A) = A (n×k), computes alpha*A*A^T + beta*C
9
+ * - `trans='transpose'`: op(A) = A^T (A stored k×n), computes alpha*A^T*A + beta*C
10
+ *
11
+ * No dedicated kernel — this is `sgemmtr`'s exact kernel
12
+ * (`sgemmtr_small.wgsl`/`sgemmtr_large.wgsl`) with `A` aliased into both
13
+ * operand slots (`B := A`).
14
+ *
15
+ * {@includeCode ../../examples/ssyrk/ssyrk.js}
16
+ *
17
+ * **Browser (standalone HTML):**
18
+ * {@includeCode ../../examples/ssyrk/web/ssyrk.html}
19
+ *
20
+ * @param device - GPUDevice from `init()`
21
+ * @param uplo - `'lower'` to update only `col <= row`, `'upper'` for `col >= row`
22
+ * @param trans - `'no-transpose'` for A*A^T, `'transpose'` for A^T*A
23
+ * @param n - order of C (C is n×n); rows of op(A)
24
+ * @param k - columns of op(A) — the shared/contracted dimension
25
+ * @param alpha - scalar multiplier for op(A)*op(A)^T
26
+ * @param A - Float32Array, row-major or column-major (see `layout`)
27
+ * @param lda - leading dimension of A as stored
28
+ * @param beta - scalar multiplier for C
29
+ * @param C - Float32Array input/output matrix, row-major or column-major
30
+ * @param ldc - leading dimension of C as stored
31
+ * @param layout - storage layout shared by A/C when they're Float32Array
32
+ * (default: `'row-major'`) — same handling as `sgemmtr`; column-major C
33
+ * flips `uplo` internally so it still names the triangle you asked for
34
+ * @returns updated C as a Float32Array (only the requested triangle changed)
35
+ * @see <a href="https://github.com/manit2004/wgblas/blob/main/src/ssyrk/ssyrk.mjs#L21">Source code: ssyrk.mjs (L21)</a>
36
+ * @category BLAS Level 3
37
+ */
38
+ export declare function ssyrk(
39
+ device: GPUDevice,
40
+ uplo: 'lower' | 'upper',
41
+ trans: 'no-transpose' | 'transpose',
42
+ n: number,
43
+ k: number,
44
+ alpha: number,
45
+ A: Float32Array,
46
+ lda: number,
47
+ beta: number,
48
+ C: Float32Array,
49
+ ldc: number,
50
+ layout?: 'row-major' | 'column-major',
51
+ ): Promise<{ C: Float32Array; gpuTimeMs?: number }>;
52
+
53
+ /**
54
+ * Performs the symmetric rank-k update C := uplo(alpha * op(A) * op(A)^T + beta * C)
55
+ *
56
+ * A and C are both kept GPU-resident. Each matrix's own `layout` (set at
57
+ * `GpuMatrix.from` time) determines the operation — there is no separate
58
+ * `layout` argument here. A must be a GpuMatrix whenever C is, and vice
59
+ * versa — mixing a GpuMatrix with a plain Float32Array is not supported.
60
+ *
61
+ * {@includeCode ../../examples/ssyrk/gpu.ssyrk.js}
62
+ *
63
+ * @param device - GPUDevice from `init()`
64
+ * @param uplo - `'lower'` to update only `col <= row`, `'upper'` for `col >= row`
65
+ * @param trans - `'no-transpose'` for A*A^T, `'transpose'` for A^T*A
66
+ * @param n - order of C (C is n×n); rows of op(A)
67
+ * @param k - columns of op(A) — the shared/contracted dimension
68
+ * @param alpha - scalar multiplier for op(A)*op(A)^T
69
+ * @param A - GpuMatrix
70
+ * @param lda - leading dimension of A (must equal A.lda)
71
+ * @param beta - scalar multiplier for C
72
+ * @param C - GpuMatrix (mutated in place; only the requested triangle changes)
73
+ * @param ldc - leading dimension of C (must equal C.lda)
74
+ * @returns no C — it stays GPU-resident; call `C.read()` yourself for a CPU readback (see the example)
75
+ * @see <a href="https://github.com/manit2004/wgblas/blob/main/src/ssyrk/ssyrk.mjs#L21">Source code: ssyrk.mjs (L21)</a>
76
+ * @category BLAS Level 3
77
+ */
78
+ export declare function ssyrk(
79
+ device: GPUDevice,
80
+ uplo: 'lower' | 'upper',
81
+ trans: 'no-transpose' | 'transpose',
82
+ n: number,
83
+ k: number,
84
+ alpha: number,
85
+ A: GpuMatrix,
86
+ lda: number,
87
+ beta: number,
88
+ C: GpuMatrix,
89
+ ldc: number,
90
+ ): Promise<{ gpuTimeMs?: number }>;
@@ -0,0 +1,177 @@
1
+ import {
2
+ uploadBuffer,
3
+ createStorageBuffer,
4
+ createParamsBuffer,
5
+ stageReadback,
6
+ destroyBuffers,
7
+ } from "../util/buffer.mjs";
8
+ import { createBindGroup } from "../util/bindgroup.mjs";
9
+ import { beginTimedEncoder, encodePass, submit } from "../util/compute.mjs";
10
+ import { extractResult } from "../util/result.mjs";
11
+ import { resolveTimestamp, extractTimestamp } from "../util/benchmark.mjs";
12
+ import { getPipeline } from "../util/pipeline.mjs";
13
+ import { GpuMatrix } from "../classes/GpuMatrix.mjs";
14
+ import { requireWorkgroupCount } from "../util/workgroup.mjs";
15
+ import { BM_SMALL, BN_SMALL, BM_LARGE, BN_LARGE, LARGE_TILE_WORKGROUP_THRESHOLD } from "../util/constants.mjs";
16
+ import { requireSameDevice } from "../util/device.mjs";
17
+
18
+
19
+ // ssyrk: C := uplo(alpha*op(A)*op(A)^T + beta*C). No dedicated shader —
20
+ // sgemmtr's kernel with A duplicated into a separate B buffer (B := A).
21
+ export async function ssyrk(
22
+ device, uplo, trans, n, k, alpha, A, lda, beta, C, ldc, layout = "row-major",
23
+ ) {
24
+ const AIsGpu = A instanceof GpuMatrix;
25
+ const CIsGpu = C instanceof GpuMatrix;
26
+
27
+ if (!(device instanceof GPUDevice))
28
+ throw new Error("device must be a GPUDevice.");
29
+ requireSameDevice(device, "ssyrk", { A, C });
30
+ if (uplo !== "lower" && uplo !== "upper")
31
+ throw new Error("uplo must be 'lower' or 'upper'.");
32
+ if (trans !== "no-transpose" && trans !== "transpose")
33
+ throw new Error("trans must be 'no-transpose' or 'transpose'.");
34
+ if (layout !== "row-major" && layout !== "column-major")
35
+ throw new Error("layout must be 'row-major' or 'column-major'.");
36
+ if (typeof alpha !== "number")
37
+ throw new Error("alpha must be a number.");
38
+ if (Number.isNaN(alpha)) throw new Error("alpha must not be NaN.");
39
+ if (!Number.isFinite(alpha)) throw new Error("alpha must be finite.");
40
+ if (typeof beta !== "number")
41
+ throw new Error("beta must be a number.");
42
+ if (Number.isNaN(beta)) throw new Error("beta must not be NaN.");
43
+ if (!Number.isFinite(beta)) throw new Error("beta must be finite.");
44
+ if (!Number.isInteger(n) || !Number.isInteger(k) || !Number.isInteger(lda) || !Number.isInteger(ldc))
45
+ throw new Error("n, k, lda, and ldc must be integers.");
46
+ if (!AIsGpu && !(A instanceof Float32Array))
47
+ throw new Error("A must be a Float32Array or GpuMatrix.");
48
+ if (!CIsGpu && !(C instanceof Float32Array))
49
+ throw new Error("C must be a Float32Array or GpuMatrix.");
50
+ if (AIsGpu && !CIsGpu)
51
+ throw new Error("C must be a GpuMatrix when A is a GpuMatrix.");
52
+ if (CIsGpu && !AIsGpu)
53
+ throw new Error("A must be a GpuMatrix when C is a GpuMatrix.");
54
+ if (n < 0 || k < 0) throw new Error("n and k must be non-negative.");
55
+ if (n === 0) return CIsGpu ? {} : { C };
56
+
57
+ // GpuMatrix's own .layout wins over the shared `layout` argument.
58
+ const effLayoutA = AIsGpu ? A.layout : layout;
59
+ const effLayoutC = CIsGpu ? C.layout : layout;
60
+
61
+ // A: op(A) is n x k; A itself is n x k or k x n depending on trans.
62
+ const aRows = effLayoutA === "column-major" ? k : n;
63
+ const aCols = effLayoutA === "column-major" ? n : k;
64
+ const aOuter = trans === "no-transpose" ? aRows : aCols;
65
+ const aInner = trans === "no-transpose" ? aCols : aRows;
66
+ if (lda < aInner)
67
+ throw new Error(`lda must be >= ${effLayoutA === "column-major" ? "rows" : "cols"} of A as stored.`);
68
+ if (AIsGpu) {
69
+ if (lda !== A.lda) throw new Error("lda must match A.lda when A is a GpuMatrix.");
70
+ const [aLogRows, aLogCols] = trans === "no-transpose" ? [n, k] : [k, n];
71
+ if (A.rows < aLogRows || A.cols < aLogCols)
72
+ throw new Error("A is too small for the given n, k, and trans.");
73
+ } else if (A.length < (aOuter - 1) * lda + aInner) {
74
+ throw new Error("A does not have enough elements for the given dimensions and lda.");
75
+ }
76
+
77
+ // C: always n x n symmetric — layout only affects storage order, not size.
78
+ if (ldc < n) throw new Error("ldc must be >= n.");
79
+ if (CIsGpu) {
80
+ if (ldc !== C.lda) throw new Error("ldc must match C.lda when C is a GpuMatrix.");
81
+ if (C.rows < n || C.cols < n) throw new Error("C is too small for the given n.");
82
+ } else if (C.length < (n - 1) * ldc + n) {
83
+ throw new Error("C does not have enough elements for the given dimensions and ldc.");
84
+ }
85
+
86
+ // Column-major A reinterpreted row-major is A^T — flip trans, same trick sgemm/sgemmtr use.
87
+ let transA = trans;
88
+ if (effLayoutA === "column-major")
89
+ transA = transA === "no-transpose" ? "transpose" : "no-transpose";
90
+ // op(B) := op(A)^T (B is A itself) — opposite of transA.
91
+ let transB = transA === "no-transpose" ? "transpose" : "no-transpose";
92
+
93
+ // Column-major C: same swap trick sgemm/sgemmtr use, simplified since A === B and m=n.
94
+ let uploEff = uplo;
95
+ if (effLayoutC === "column-major") {
96
+ [transA, transB] = [
97
+ transB === "no-transpose" ? "transpose" : "no-transpose",
98
+ transA === "no-transpose" ? "transpose" : "no-transpose",
99
+ ];
100
+ uploEff = uploEff === "lower" ? "upper" : "lower";
101
+ }
102
+
103
+ // Shape-based auto-select — see sgemmtr_small.wgsl/sgemmtr_large.wgsl. m=n=n here (square C).
104
+ const largeWgX = Math.ceil(n / BN_LARGE);
105
+ const largeWgY = Math.ceil(n / BM_LARGE);
106
+ const useLargeTile = largeWgX * largeWgY >= LARGE_TILE_WORKGROUP_THRESHOLD;
107
+
108
+ const pipeline = await getPipeline(device, useLargeTile ? "sgemmtr_large" : "sgemmtr_small");
109
+
110
+ const ABuffer = AIsGpu ? A._buf : uploadBuffer(device, A, "ssyrk-A", false);
111
+ const CBuffer = CIsGpu ? C._buf : uploadBuffer(device, C, "ssyrk-C", true);
112
+ // B := A, but as a genuinely separate buffer — re-uploaded for Float32Array,
113
+ // GPU-copied (see below) for GpuMatrix, since the caller owns ABuffer.
114
+ const BBuffer = AIsGpu
115
+ ? createStorageBuffer(device, ABuffer.size, "ssyrk-B", GPUBufferUsage.COPY_DST)
116
+ : uploadBuffer(device, A, "ssyrk-B", false);
117
+ const paramsBuffer = createParamsBuffer(device,
118
+ [
119
+ { value: n, type: "u32" }, // gemmtr's m
120
+ { value: n, type: "u32" }, // gemmtr's n
121
+ { value: k, type: "u32" },
122
+ { value: alpha, type: "f32" },
123
+ { value: beta, type: "f32" },
124
+ { value: lda, type: "u32" }, // gemmtr's lda
125
+ { value: lda, type: "u32" }, // gemmtr's ldb — B := A, same lda
126
+ { value: ldc, type: "u32" },
127
+ { value: transA === "transpose" ? 1 : 0, type: "u32" },
128
+ { value: transB === "transpose" ? 1 : 0, type: "u32" },
129
+ { value: uploEff === "upper" ? 1 : 0, type: "u32" },
130
+ ],
131
+ "ssyrk-params",
132
+ );
133
+
134
+ try {
135
+ const bindGroup = createBindGroup(device, pipeline.getBindGroupLayout(0), [
136
+ ABuffer,
137
+ BBuffer,
138
+ CBuffer,
139
+ paramsBuffer,
140
+ ]);
141
+
142
+ const wgCount = useLargeTile
143
+ ? {
144
+ x: requireWorkgroupCount(device, largeWgX, "ssyrk", "x"),
145
+ y: requireWorkgroupCount(device, largeWgY, "ssyrk", "y"),
146
+ }
147
+ : {
148
+ x: requireWorkgroupCount(device, Math.ceil(n / BN_SMALL), "ssyrk", "x"),
149
+ y: requireWorkgroupCount(device, Math.ceil(n / BM_SMALL), "ssyrk", "y"),
150
+ };
151
+ // Manual encoder (not runComputePass) so the A->B duplicate copy lands
152
+ // on the same command encoder, strictly before the compute pass reads B.
153
+ const { commandEncoder, querySet, passDescriptor } = beginTimedEncoder(device);
154
+ if (AIsGpu) commandEncoder.copyBufferToBuffer(ABuffer, 0, BBuffer, 0, ABuffer.size);
155
+ encodePass(commandEncoder, pipeline, bindGroup, wgCount, passDescriptor);
156
+ const ts = resolveTimestamp(device, commandEncoder, querySet);
157
+ const readBuffer = CIsGpu ? null : stageReadback(device, commandEncoder, CBuffer);
158
+
159
+ submit(device, commandEncoder);
160
+
161
+ const gpuTimeMs = await extractTimestamp(ts);
162
+
163
+ if (CIsGpu) {
164
+ if (gpuTimeMs !== undefined) return { gpuTimeMs };
165
+ return {};
166
+ }
167
+
168
+ const result = await extractResult(readBuffer, Float32Array);
169
+ if (gpuTimeMs !== undefined) return { C: result, gpuTimeMs };
170
+ return { C: result };
171
+ } finally {
172
+ if (!AIsGpu) destroyBuffers(ABuffer);
173
+ destroyBuffers(BBuffer); // always our own buffer, never the caller's
174
+ if (!CIsGpu) destroyBuffers(CBuffer);
175
+ destroyBuffers(paramsBuffer);
176
+ }
177
+ }