Commit ce8b9ee
feat(bert): wire ScratchPool into the embedding hot path
Two changes that activate SKaiNET 0.21.0's ScratchPool SPI for the BERT
encoder workload:
1. Wrap BertRuntime.forward in ctx.scratch.scope { ... }. Upstream
SIMD kernels (matmul, dequant) acquire workspace from ctx.scratch
internally; the scope drains acquired buffers back to the pool on
exit. With the default NoopScratchPool this is a pass-through; with a
real pool it eliminates per-forward FloatArray allocations on what is
typically the busiest path for an embedding workload (encode() called
many times in a row).
2. Add PooledExecutionContext — a thin ExecutionContext delegate that
provides a SizeClassedScratchPool. Wire it as the default ctx in
KBertJava.loadSafeTensors, since Java embedding consumers virtually
always batch many encode() calls.
Default behavior is preserved: callers that construct BertRuntime with
a plain DirectCpuExecutionContext (no PooledExecutionContext wrapper)
continue to use NoopScratchPool and see no change.
22/22 BertRuntime + BertNumericalAccuracy + HuggingFaceTokenizer tests
green on JDK 25.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 2da0a2a commit ce8b9ee
3 files changed
Lines changed: 67 additions & 20 deletions
File tree
- llm-inference/bert/src
- commonMain/kotlin/sk/ainet/models/bert
- jvmMain/kotlin/sk/ainet/models/bert/java
Lines changed: 26 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
129 | 139 | | |
130 | 140 | | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | 141 | | |
135 | 142 | | |
136 | 143 | | |
| |||
Lines changed: 35 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
Lines changed: 6 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
49 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
50 | 55 | | |
51 | 56 | | |
52 | 57 | | |
| |||
0 commit comments