doppler-gpu 0.4.15 → 0.5.1

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 (221) hide show
  1. package/README.md +9 -0
  2. package/package.json +25 -2
  3. package/src/cli/doppler-cli.js +253 -94
  4. package/src/cli/runtime-profiles.js +3 -7
  5. package/src/client/doppler-api.d.ts +28 -0
  6. package/src/client/doppler-api.js +2 -0
  7. package/src/client/inspection.d.ts +122 -0
  8. package/src/client/inspection.js +343 -0
  9. package/src/client/runtime/index.d.ts +54 -0
  10. package/src/client/runtime/index.js +190 -18
  11. package/src/client/runtime/model-manager.d.ts +0 -4
  12. package/src/client/runtime/model-manager.js +0 -28
  13. package/src/client/runtime/model-session.d.ts +48 -1
  14. package/src/client/runtime/model-session.js +149 -30
  15. package/src/client/runtime/model-source.d.ts +10 -1
  16. package/src/client/runtime/scoped-session.d.ts +117 -0
  17. package/src/client/runtime/scoped-session.js +257 -0
  18. package/src/client/runtime/types.d.ts +0 -1
  19. package/src/client/runtime/types.js +0 -1
  20. package/src/config/evidence/boundary-evidence-policy.json +54 -0
  21. package/src/config/evidence/source-intake-policy.json +122 -0
  22. package/src/config/inspection/observation-policies.json +80 -0
  23. package/src/config/param-validator.js +21 -36
  24. package/src/config/runtime/compute/f16-activations.json +1 -0
  25. package/src/config/runtime/compute/f16-batched.json +1 -0
  26. package/src/config/runtime/default.json +1 -0
  27. package/src/config/runtime/diagnostics/debug-logits.json +1 -0
  28. package/src/config/runtime/experiments/bench/gemma3-bench-q4k-alt.json +1 -0
  29. package/src/config/runtime/experiments/bench/gemma3-bench-q4k-batched.json +1 -0
  30. package/src/config/runtime/experiments/bench/gemma3-bench-q4k.json +1 -0
  31. package/src/config/runtime/experiments/bench/gemma4-bench-q4k.json +1 -0
  32. package/src/config/runtime/experiments/bench/qwen3-bench-q4k-alt.json +1 -0
  33. package/src/config/runtime/experiments/bench/qwen3-bench-q4k-batched.json +1 -0
  34. package/src/config/runtime/experiments/debug/gemma3-browser-diagnose-q4k.json +1 -1
  35. package/src/config/runtime/experiments/debug/gemma3-debug-q4k.json +1 -3
  36. package/src/config/runtime/experiments/debug/gemma4-e2b-browser-diagnose-q4k.json +1 -1
  37. package/src/config/runtime/experiments/debug/qwen35-browser-diagnose-q4k.json +1 -1
  38. package/src/config/runtime/experiments/verify/gemma3-verify.json +1 -3
  39. package/src/config/runtime/experiments/verify/gemma4-e2b-verify.json +1 -3
  40. package/src/config/runtime/experiments/verify/lfm2-verify.json +1 -3
  41. package/src/config/runtime/experiments/verify/translategemma-verify.json +1 -3
  42. package/src/config/runtime/model/gemma2-debug.json +1 -0
  43. package/src/config/runtime/model/gemma2-pipeline-debug.json +1 -0
  44. package/src/config/runtime/model/gemma2-pipeline.json +1 -0
  45. package/src/config/runtime/model/gemma3-layer-probe.json +1 -0
  46. package/src/config/runtime/model/qwen3-5-layer-probe.json +1 -0
  47. package/src/config/runtime/model/qwen3-5-layer3-boundary-probe.json +1 -0
  48. package/src/config/runtime/model/qwen3-5-linear-attn-debug.json +1 -0
  49. package/src/config/runtime/optimization-candidates/registry.json +5 -0
  50. package/src/config/runtime/platform/metal-apple-q4k.json +1 -0
  51. package/src/config/runtime/profiles/default.json +1 -0
  52. package/src/config/runtime/profiles/diffusiongemma-26b-a4b-throughput.json +1 -0
  53. package/src/config/runtime/profiles/gemma3-270m-q4k-throughput-overlapped-probe.json +1 -0
  54. package/src/config/runtime/profiles/gemma3-270m-q4k-throughput-probe.json +1 -0
  55. package/src/config/runtime/profiles/gemma4-12b-rdrr-safe-b4.json +1 -3
  56. package/src/config/runtime/profiles/gemma4-12b-rdrr-safe-single.json +1 -3
  57. package/src/config/runtime/profiles/gemma4-31b-f16-activations-probe.json +1 -5
  58. package/src/config/runtime/profiles/gemma4-e2b-baseline-probe.json +1 -3
  59. package/src/config/runtime/profiles/gemma4-e2b-fused-rmsnorm-probe.json +1 -3
  60. package/src/config/runtime/profiles/gemma4-e2b-fused-rmsnorm-test.json +1 -0
  61. package/src/config/runtime/profiles/gemma4-e2b-int4ple-baseline-probe.json +1 -5
  62. package/src/config/runtime/profiles/gemma4-e2b-int4ple-f16a-probe.json +1 -5
  63. package/src/config/runtime/profiles/gemma4-e2b-int4ple-reference-export.json +1 -3
  64. package/src/config/runtime/profiles/gemma4-e2b-int4ple-retain-q4k-probe.json +1 -5
  65. package/src/config/runtime/profiles/gemma4-e2b-low-memory.json +1 -0
  66. package/src/config/runtime/profiles/gemma4-e2b-prefill-profile.json +1 -3
  67. package/src/config/runtime/profiles/gemma4-e2b-throughput-probe.json +1 -0
  68. package/src/config/runtime/profiles/low-memory.json +1 -0
  69. package/src/config/runtime/profiles/production.json +1 -3
  70. package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-attn128-candidate.json +1 -0
  71. package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-maxbatch64-candidate.json +1 -0
  72. package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-normed-ffn-candidate.json +1 -0
  73. package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-overlap-ringtokens1-candidate.json +1 -0
  74. package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-parity.json +1 -0
  75. package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-sequential-candidate.json +1 -0
  76. package/src/config/runtime/profiles/qwen-3-5-0-8b-metal-throughput.json +1 -0
  77. package/src/config/runtime/profiles/qwen-3-5-0-8b-prefill-profile.json +1 -3
  78. package/src/config/runtime/profiles/qwen-3-5-0-8b-throughput-probe.json +1 -0
  79. package/src/config/runtime/profiles/qwen-3-5-2b-f16-primary-throughput.json +1 -5
  80. package/src/config/runtime/profiles/qwen-3-5-2b-metal-parity.json +1 -0
  81. package/src/config/runtime/profiles/qwen-3-5-2b-metal-throughput.json +1 -0
  82. package/src/config/runtime/profiles/qwen-3-5-9b-metal-correctness.json +1 -0
  83. package/src/config/runtime/profiles/qwen-3-embedding-0-6b-metal-stability.json +1 -3
  84. package/src/config/runtime/profiles/qwen-3-embedding-0-6b-metal-throughput.json +1 -0
  85. package/src/config/runtime/profiles/qwen-3-reranker-0-6b-metal-stability.json +1 -0
  86. package/src/config/runtime/profiles/qwen-3-reranker-0-6b-metal-throughput.json +1 -0
  87. package/src/config/runtime/profiles/qwen3-6-27b-f16-activations-probe.json +1 -5
  88. package/src/config/runtime/profiles/rerank-stability.json +1 -3
  89. package/src/config/runtime/profiles/rerank-throughput.json +1 -3
  90. package/src/config/runtime/profiles/simulation.json +1 -3
  91. package/src/config/runtime/profiles/throughput.json +1 -3
  92. package/src/config/runtime/profiles/trace-layers.json +1 -3
  93. package/src/config/runtime/profiles/translategemma-1b-nativekd2-q4k-rdna3-throughput-probe.json +1 -0
  94. package/src/config/runtime/profiles/turboquant-contiguous-prod.json +1 -0
  95. package/src/config/runtime/profiles/turboquant-contiguous.json +1 -0
  96. package/src/config/runtime/profiles/turboquant.json +1 -0
  97. package/src/config/runtime/profiles/vector-stability.json +1 -3
  98. package/src/config/runtime/profiles/vector-throughput.json +1 -0
  99. package/src/config/runtime/profiles/verbose-trace.json +1 -3
  100. package/src/config/runtime/tiers/gemma4-128gb.json +1 -0
  101. package/src/config/runtime/tiers/gemma4-16gb.json +1 -0
  102. package/src/config/runtime/tiers/gemma4-24gb.json +1 -0
  103. package/src/config/runtime/tiers/gemma4-32gb.json +1 -0
  104. package/src/config/schema/benchmark.schema.d.ts +7 -0
  105. package/src/config/schema/benchmark.schema.js +5 -0
  106. package/src/config/schema/index.d.ts +8 -16
  107. package/src/config/schema/index.js +5 -8
  108. package/src/config/schema/kernel-thresholds.schema.d.ts +0 -12
  109. package/src/config/schema/kernel-thresholds.schema.js +0 -12
  110. package/src/config/schema/kernel-warmup.schema.d.ts +1 -3
  111. package/src/config/schema/kernel-warmup.schema.js +0 -1
  112. package/src/config/schema/runtime-profile.schema.d.ts +24 -0
  113. package/src/config/schema/runtime-profile.schema.js +92 -0
  114. package/src/config/schema/shared-runtime.schema.d.ts +1 -4
  115. package/src/config/schema/shared-runtime.schema.js +0 -2
  116. package/src/config/schema/tooling.schema.d.ts +8 -5
  117. package/src/config/schema/tooling.schema.js +3 -2
  118. package/src/debug/tensor.d.ts +2 -0
  119. package/src/debug/tensor.js +21 -2
  120. package/src/gpu/command-recorder.d.ts +5 -0
  121. package/src/gpu/command-recorder.js +17 -0
  122. package/src/gpu/kernel-runtime.js +2 -10
  123. package/src/gpu/kernels/index.d.ts +0 -2
  124. package/src/gpu/kernels/index.js +0 -2
  125. package/src/gpu/kernels/kernel-prewarm.d.ts +3 -0
  126. package/src/gpu/kernels/kernel-prewarm.js +60 -0
  127. package/src/gpu/kernels/utils.d.ts +2 -4
  128. package/src/gpu/kernels/utils.js +2 -4
  129. package/src/index.d.ts +13 -1
  130. package/src/index.js +7 -1
  131. package/src/inference/browser-harness-model-helpers.js +4 -0
  132. package/src/inference/browser-harness-runtime-helpers.js +25 -10
  133. package/src/inference/browser-harness-text-helpers.js +4 -1
  134. package/src/inference/browser-harness.d.ts +4 -0
  135. package/src/inference/browser-harness.js +91 -4
  136. package/src/inference/observation-context.d.ts +22 -0
  137. package/src/inference/observation-context.js +41 -0
  138. package/src/inference/pipelines/text/attention/attn-config.js +1 -1
  139. package/src/inference/pipelines/text/attention/executor-immediate.js +42 -0
  140. package/src/inference/pipelines/text/attention/executor-recorded.js +29 -0
  141. package/src/inference/pipelines/text/attention/index.js +7 -0
  142. package/src/inference/pipelines/text/attention/interpreter.js +1334 -0
  143. package/src/inference/pipelines/text/attention/plan.d.ts +36 -0
  144. package/src/inference/pipelines/text/attention/plan.js +328 -0
  145. package/src/inference/pipelines/text/attention/receipt.js +60 -0
  146. package/src/inference/pipelines/text/attention/record.js +8 -1278
  147. package/src/inference/pipelines/text/attention/run.js +8 -1386
  148. package/src/inference/pipelines/text/generator-decode-policy.js +4 -1
  149. package/src/inference/pipelines/text/generator-helpers.js +2 -0
  150. package/src/inference/pipelines/text/generator-steps.js +32 -15
  151. package/src/inference/pipelines/text/generator.js +10 -5
  152. package/src/inference/pipelines/text/init.d.ts +3 -0
  153. package/src/inference/pipelines/text/layer-plan-gpu.js +2 -0
  154. package/src/inference/pipelines/text/layer.js +2 -0
  155. package/src/inference/pipelines/text/model-load.d.ts +0 -1
  156. package/src/inference/pipelines/text/model-load.js +2 -11
  157. package/src/inference/pipelines/text/probes.js +10 -0
  158. package/src/inference/pipelines/text/resolved-runtime-session.d.ts +56 -0
  159. package/src/inference/pipelines/text/resolved-runtime-session.js +175 -0
  160. package/src/inference/pipelines/text/state.d.ts +6 -0
  161. package/src/inference/pipelines/text/state.js +3 -0
  162. package/src/inference/pipelines/text/types.d.ts +6 -0
  163. package/src/inference/pipelines/text.js +16 -1
  164. package/src/inference/refactor-receipt.d.ts +49 -0
  165. package/src/inference/refactor-receipt.js +188 -0
  166. package/src/inference/resource-scope.d.ts +33 -0
  167. package/src/inference/resource-scope.js +168 -0
  168. package/src/inference/test-harness.d.ts +3 -0
  169. package/src/inference/test-harness.js +1 -0
  170. package/src/storage/model-cache.d.ts +53 -0
  171. package/src/storage/model-cache.js +550 -0
  172. package/src/tooling/boundary-evidence.d.ts +51 -0
  173. package/src/tooling/boundary-evidence.js +408 -0
  174. package/src/tooling/browser-command-runner.js +4 -4
  175. package/src/tooling/command-context.d.ts +25 -0
  176. package/src/tooling/command-context.js +53 -0
  177. package/src/tooling/command-runner-shared.js +22 -5
  178. package/src/tooling/demo-receipts.d.ts +56 -0
  179. package/src/tooling/demo-receipts.js +113 -0
  180. package/src/tooling/diagnose-runner.js +2 -2
  181. package/src/tooling/execution-cost-ledger.d.ts +21 -0
  182. package/src/tooling/execution-cost-ledger.js +199 -0
  183. package/src/tooling/node-command-runner.js +2 -2
  184. package/src/tooling/opfs-cache.d.ts +11 -49
  185. package/src/tooling/opfs-cache.js +4 -510
  186. package/src/tooling/precision-replay-math.d.ts +42 -0
  187. package/src/tooling/precision-replay-math.js +174 -0
  188. package/src/tooling/program-bundle-parity.d.ts +3 -0
  189. package/src/tooling/program-bundle-parity.js +8 -0
  190. package/src/tooling/program-bundle.d.ts +1 -52
  191. package/src/tooling/program-bundle.js +0 -139
  192. package/src/tooling/registered-variant-calibration.d.ts +24 -0
  193. package/src/tooling/registered-variant-calibration.js +286 -0
  194. package/src/tooling/runtime-input-composition.js +105 -53
  195. package/src/tooling/runtime-intent.js +41 -0
  196. package/src/tooling/runtime-optimization-index.d.ts +6 -0
  197. package/src/tooling/runtime-optimization-index.js +72 -0
  198. package/src/tooling/runtime-optimization.d.ts +47 -6
  199. package/src/tooling/runtime-optimization.js +559 -40
  200. package/src/tooling/source-intake.d.ts +49 -0
  201. package/src/tooling/source-intake.js +414 -0
  202. package/src/tooling-exports/evidence.d.ts +34 -0
  203. package/src/tooling-exports/evidence.js +34 -0
  204. package/src/tooling-exports/runtime.d.ts +7 -0
  205. package/src/tooling-exports/runtime.js +7 -0
  206. package/src/tooling-exports.d.ts +11 -0
  207. package/src/tooling-exports.js +6 -3
  208. package/src/tooling-exports.shared.d.ts +32 -0
  209. package/src/tooling-exports.shared.js +31 -0
  210. package/src/version.js +1 -1
  211. package/src/config/schema/tuner.schema.d.ts +0 -39
  212. package/src/config/schema/tuner.schema.js +0 -13
  213. package/src/gpu/kernel-tuner/benchmarks.js +0 -902
  214. package/src/gpu/kernel-tuner/cache.js +0 -145
  215. package/src/gpu/kernel-tuner/index.js +0 -38
  216. package/src/gpu/kernel-tuner/tuner.js +0 -247
  217. package/src/gpu/kernel-tuner.js +0 -12
  218. package/src/gpu/kernels/kernel-tuning.d.ts +0 -42
  219. package/src/gpu/kernels/kernel-tuning.js +0 -158
  220. package/src/gpu/profiler.js +0 -382
  221. package/src/inference/pipelines/text/attention/output-gate-fusion.js +0 -37
@@ -1,382 +0,0 @@
1
-
2
-
3
- import { getDevice, hasFeature, FEATURES } from './device.js';
4
- import { allowReadback } from './perf-guards.js';
5
- import { log } from '../debug/index.js';
6
- import { getRuntimeConfig } from '../config/runtime.js';
7
- import { computeBasicStats } from '../debug/stats.js';
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
- export class GPUProfiler {
19
-
20
- #device;
21
-
22
- #hasTimestampQuery;
23
-
24
- // Query set for timestamp queries (if supported)
25
-
26
- #querySet = null;
27
-
28
- #queryBuffer = null;
29
-
30
- #readbackBuffer = null;
31
-
32
- #queryCapacity = 0;
33
-
34
- #maxSamples = 0;
35
-
36
- #maxDurationMs = 0;
37
-
38
- // Maximum number of distinct labels tracked before oldest are evicted
39
- #maxHistoryLabels = 0;
40
-
41
- // Tracking state
42
-
43
- #activeLabels = new Map();
44
-
45
- #nextQueryIndex = 0;
46
-
47
- #pendingResolves = [];
48
-
49
- // Results storage
50
-
51
- #results = new Map();
52
-
53
- // CPU fallback timing
54
-
55
- #cpuTimings = new Map();
56
-
57
-
58
- constructor(device = null) {
59
- this.#device = device || getDevice();
60
- this.#hasTimestampQuery = this.#device?.features?.has(FEATURES.TIMESTAMP_QUERY) ?? false;
61
- const runtimeProfiler = getRuntimeConfig().shared?.debug?.profiler;
62
- if (!runtimeProfiler) {
63
- throw new Error('runtime.shared.debug.profiler is required.');
64
- }
65
- this.#queryCapacity = runtimeProfiler.queryCapacity;
66
- this.#maxSamples = runtimeProfiler.maxSamples;
67
- this.#maxDurationMs = runtimeProfiler.maxDurationMs;
68
- this.#maxHistoryLabels = requirePositiveInteger(
69
- runtimeProfiler.maxHistoryLabels,
70
- 'runtime.shared.debug.profiler.maxHistoryLabels'
71
- );
72
-
73
- // Initialize query resources if timestamp queries available
74
- if (this.#hasTimestampQuery && this.#device) {
75
- this.#initQueryResources();
76
- }
77
- }
78
-
79
-
80
- #initQueryResources() {
81
- if (!this.#device) return;
82
-
83
- try {
84
- this.#querySet = this.#device.createQuerySet({
85
- type: 'timestamp',
86
- count: this.#queryCapacity * 2, // Start and end for each measurement
87
- });
88
-
89
- // Buffer to hold query results (8 bytes per timestamp)
90
- this.#queryBuffer = this.#device.createBuffer({
91
- size: this.#queryCapacity * 2 * 8,
92
- usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC,
93
- });
94
-
95
- // Readback buffer
96
- this.#readbackBuffer = this.#device.createBuffer({
97
- size: this.#queryCapacity * 2 * 8,
98
- usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
99
- });
100
- } catch (e) {
101
- log.warn('GPUProfiler', `Failed to create timestamp query resources: ${e}`);
102
- this.#hasTimestampQuery = false;
103
- }
104
- }
105
-
106
-
107
- begin(label) {
108
- if (this.#activeLabels.has(label)) {
109
- log.warn('GPUProfiler', `Label "${label}" already active`);
110
- return;
111
- }
112
-
113
- const startTime = performance.now();
114
-
115
- // CPU timing for begin/end; GPU timestamps require writeTimestamp() in a pass.
116
- this.#activeLabels.set(label, {
117
- cpuStartTime: startTime,
118
- });
119
- }
120
-
121
-
122
- end(label) {
123
- const active = this.#activeLabels.get(label);
124
- if (!active) {
125
- log.warn('GPUProfiler', `No active measurement for label "${label}"`);
126
- return;
127
- }
128
-
129
- const endTime = performance.now();
130
- this.#activeLabels.delete(label);
131
-
132
- if (this.#hasTimestampQuery && 'startQueryIndex' in active) {
133
- // GPU timing will be resolved later
134
- this.#pendingResolves.push({
135
- label,
136
- startIndex: active.startQueryIndex,
137
- endIndex: active.startQueryIndex + 1,
138
- cpuStartTime: active.cpuStartTime,
139
- cpuEndTime: endTime,
140
- });
141
- } else {
142
- // CPU fallback - record immediately
143
- this.#recordResult(label, endTime - active.cpuStartTime);
144
- }
145
- }
146
-
147
-
148
- writeTimestamp(pass, label, isEnd = false) {
149
- if (!this.#hasTimestampQuery || !this.#querySet) return;
150
-
151
-
152
- let queryIndex;
153
- if (!isEnd) {
154
- // Start timestamp
155
- queryIndex = this.#nextQueryIndex;
156
- this.#nextQueryIndex += 2;
157
- this.#activeLabels.set(label, {
158
- startQueryIndex: queryIndex,
159
- cpuStartTime: performance.now(),
160
- });
161
- } else {
162
- // End timestamp
163
- const active = this.#activeLabels.get(label);
164
- if (!active || !('startQueryIndex' in active)) return;
165
- queryIndex = active.startQueryIndex + 1;
166
- this.#activeLabels.delete(label);
167
- this.#pendingResolves.push({
168
- label,
169
- startIndex: active.startQueryIndex,
170
- endIndex: queryIndex,
171
- cpuStartTime: active.cpuStartTime,
172
- cpuEndTime: performance.now(),
173
- });
174
- }
175
-
176
- // Note: writeTimestamp is deprecated in modern WebGPU spec but still works in Chrome
177
- // Future: migrate to timestampWrites in GPUComputePassDescriptor
178
- (pass).writeTimestamp(this.#querySet, queryIndex);
179
- }
180
-
181
-
182
- async resolve() {
183
- if (!this.#hasTimestampQuery || this.#pendingResolves.length === 0) {
184
- return;
185
- }
186
-
187
- if (!this.#device || !this.#querySet || !this.#queryBuffer || !this.#readbackBuffer) {
188
- log.warn('GPUProfiler', 'Missing required resources for resolve');
189
- this.#pendingResolves = [];
190
- this.#nextQueryIndex = 0;
191
- return;
192
- }
193
-
194
- const encoder = this.#device.createCommandEncoder();
195
-
196
- // Resolve all timestamps to buffer
197
- const maxIndex = Math.max(...this.#pendingResolves.map(p => p.endIndex)) + 1;
198
- encoder.resolveQuerySet(this.#querySet, 0, maxIndex, this.#queryBuffer, 0);
199
-
200
- // Copy to readback buffer
201
- encoder.copyBufferToBuffer(
202
- this.#queryBuffer,
203
- 0,
204
- this.#readbackBuffer,
205
- 0,
206
- maxIndex * 8
207
- );
208
-
209
- this.#device.queue.submit([encoder.finish()]);
210
-
211
- let mapped = false;
212
-
213
- try {
214
- if (!allowReadback('GPUProfiler.resolve')) {
215
- return;
216
- }
217
-
218
- await this.#readbackBuffer.mapAsync(GPUMapMode.READ);
219
- mapped = true;
220
- const timestamps = new BigUint64Array(this.#readbackBuffer.getMappedRange());
221
-
222
- for (const pending of this.#pendingResolves) {
223
- const startNs = timestamps[pending.startIndex];
224
- const endNs = timestamps[pending.endIndex];
225
- const durationMs = Number(endNs - startNs) / 1_000_000;
226
-
227
- if (durationMs < 0 || durationMs > this.#maxDurationMs) {
228
- this.#recordResult(pending.label, pending.cpuEndTime - pending.cpuStartTime);
229
- } else {
230
- this.#recordResult(pending.label, durationMs);
231
- }
232
- }
233
- } finally {
234
- if (mapped) {
235
- this.#readbackBuffer.unmap();
236
- }
237
- this.#pendingResolves = [];
238
- this.#nextQueryIndex = 0;
239
- }
240
- }
241
-
242
-
243
- #recordResult(label, timeMs) {
244
- if (!this.#results.has(label)) {
245
- // Evict oldest labels when history exceeds the configured limit.
246
- // Map iteration order is insertion order, so the first key is the oldest.
247
- while (this.#results.size >= this.#maxHistoryLabels) {
248
- const oldest = this.#results.keys().next().value;
249
- this.#results.delete(oldest);
250
- }
251
- this.#results.set(label, {
252
- times: [],
253
- min: Infinity,
254
- max: -Infinity,
255
- sum: 0,
256
- count: 0,
257
- });
258
- }
259
-
260
- const result = this.#results.get(label);
261
- result.times.push(timeMs);
262
- result.min = Math.min(result.min, timeMs);
263
- result.max = Math.max(result.max, timeMs);
264
- result.sum += timeMs;
265
- result.count++;
266
-
267
- // Keep only last N samples for running average
268
- if (result.times.length > this.#maxSamples) {
269
- const removed = result.times.shift();
270
- result.sum -= removed;
271
- result.count--;
272
- // Recalculate min/max if needed (expensive, so only do occasionally)
273
- if (result.times.length % 20 === 0) {
274
- result.min = Math.min(...result.times);
275
- result.max = Math.max(...result.times);
276
- }
277
- }
278
- }
279
-
280
-
281
- getResults() {
282
-
283
- const output = {};
284
-
285
- for (const [label, data] of this.#results) {
286
- const stats = computeBasicStats(data.times);
287
- output[label] = {
288
- avg: stats.mean,
289
- min: stats.min,
290
- max: stats.max,
291
- count: stats.count,
292
- total: stats.total,
293
- };
294
- }
295
-
296
- return output;
297
- }
298
-
299
-
300
- getResult(label) {
301
- const data = this.#results.get(label);
302
- if (!data) return null;
303
-
304
- const stats = computeBasicStats(data.times);
305
- return {
306
- avg: stats.mean,
307
- min: stats.min,
308
- max: stats.max,
309
- count: stats.count,
310
- total: stats.total,
311
- };
312
- }
313
-
314
-
315
- reset() {
316
- this.#results.clear();
317
- this.#activeLabels.clear();
318
- this.#pendingResolves = [];
319
- this.#nextQueryIndex = 0;
320
- }
321
-
322
-
323
- getReport() {
324
- const results = this.getResults();
325
- const labels = Object.keys(results).sort();
326
-
327
- if (labels.length === 0) {
328
- return 'No profiling data collected';
329
- }
330
-
331
- let report = 'GPU Profiler Results\n';
332
- report += '\u2500'.repeat(60) + '\n';
333
- report += 'Label'.padEnd(30) + 'Avg (ms)'.padStart(10) + 'Min'.padStart(10) + 'Max'.padStart(10) + '\n';
334
- report += '\u2500'.repeat(60) + '\n';
335
-
336
- for (const label of labels) {
337
- const r = results[label];
338
- report += label.padEnd(30);
339
- report += r.avg.toFixed(3).padStart(10);
340
- report += r.min.toFixed(3).padStart(10);
341
- report += r.max.toFixed(3).padStart(10);
342
- report += '\n';
343
- }
344
-
345
- return report;
346
- }
347
-
348
-
349
- isGPUTimingAvailable() {
350
- return this.#hasTimestampQuery;
351
- }
352
-
353
-
354
- destroy() {
355
- if (this.#querySet) {
356
- this.#querySet.destroy();
357
- this.#querySet = null;
358
- }
359
- if (this.#queryBuffer) {
360
- this.#queryBuffer.destroy();
361
- this.#queryBuffer = null;
362
- }
363
- if (this.#readbackBuffer) {
364
- this.#readbackBuffer.destroy();
365
- this.#readbackBuffer = null;
366
- }
367
- this.#results.clear();
368
- this.#activeLabels.clear();
369
- this.#pendingResolves = [];
370
- this.#nextQueryIndex = 0;
371
- }
372
- }
373
-
374
- function requirePositiveInteger(value, label) {
375
- if (!Number.isInteger(value) || value <= 0) {
376
- throw new Error(`${label} must be a positive integer.`);
377
- }
378
- return value;
379
- }
380
-
381
-
382
- export default GPUProfiler;
@@ -1,37 +0,0 @@
1
- function kernelPathUsesHead256DecodeAttention(kernelPath) {
2
- const steps = kernelPath?.decode?.steps;
3
- if (!Array.isArray(steps)) return false;
4
- return steps.some((step) => (
5
- step?.op === 'attention'
6
- && step?.kernel === 'attention_decode_online_head256_f16kv_output_gate.wgsl'
7
- && (step?.entry === undefined || step.entry === 'main')
8
- ));
9
- }
10
-
11
- function tensorElementCount(tensor) {
12
- if (!Array.isArray(tensor?.shape)) return null;
13
- return tensor.shape.reduce((total, value) => total * value, 1);
14
- }
15
-
16
- export function canUseAttentionOutputGateFusion(options = {}) {
17
- const {
18
- session,
19
- qGateTensor,
20
- numTokens,
21
- numHeads,
22
- headDim,
23
- cachedKDtype,
24
- cachedVDtype,
25
- kernelPath,
26
- diffusionGemmaDecoder,
27
- } = options;
28
- if (diffusionGemmaDecoder === true) return false;
29
- if (session?.attentionDecodeOnline?.useOutputGateFusion !== true) return false;
30
- if (!qGateTensor?.buffer || qGateTensor.dtype !== 'f32') return false;
31
- if (numTokens !== 1 || headDim !== 256) return false;
32
- if (cachedKDtype !== 'f16' || cachedVDtype !== 'f16') return false;
33
- if (!kernelPathUsesHead256DecodeAttention(kernelPath)) return false;
34
- const gateElements = tensorElementCount(qGateTensor);
35
- if (gateElements !== null && gateElements < numTokens * numHeads * headDim) return false;
36
- return true;
37
- }