feat(scratch): add ScratchPool SPI for runtime workspace allocation#550
Merged
michalharakal merged 1 commit intodevelopfrom Apr 28, 2026
Merged
feat(scratch): add ScratchPool SPI for runtime workspace allocation#550michalharakal merged 1 commit intodevelopfrom
michalharakal merged 1 commit intodevelopfrom
Conversation
Closes #549. Adds a generic workspace allocator for short-lived FloatArray buffers used by attention scratch, RoPE tables, KV-cache slice copies, padding scratch, and any other nn workload that allocates intermediates per forward step. * `sk.ainet.lang.tensor.scratch.ScratchPool` — SPI with `acquireFloat`, `acquireFloatZeroed`, `scope { ... }`, and `stats()`. * `NoopScratchPool` — default no-op pool; every acquire allocates fresh. Bit-for-bit equivalent to pre-pool behavior. * `SizeClassedScratchPool` — power-of-two slabs starting at 64 floats; scoped lifetime; per-class cap with surplus drop. Single-threaded by intent (one forward at a time per pool); concurrent forwards use separate pools. * `ExecutionContext.scratch: ScratchPool` — backward-compatible accessor with default `NoopScratchPool`. Existing impls keep working unchanged. Out of scope here: per-thread ambient carrier (only needed where call sites don't take ExecutionContext); direct-memory variants. Bumps version to 0.21.0-SNAPSHOT for downstream composite-build integration. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
📖 Documentation Preview The documentation has been built successfully for this PR. Generated Files:
Artifacts:
This comment will be updated automatically when the PR is updated. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #549.
Summary
Adds a generic workspace allocator (
ScratchPool) for short-livedFloatArraybuffers used by attention scratch, RoPE tables, KV-cache slice copies, padding scratch, and any other nn workload that allocates intermediates per forward step.Workspace allocation is generic across nn workloads — CNN, encoder, embedding, training-time gradient buffers all need short-lived intermediates. Routing those through a pool eliminates per-step allocation pressure on the GC heap.
What's added
sk.ainet.lang.tensor.scratch.ScratchPool— SPI withacquireFloat,acquireFloatZeroed,scope { ... }, andstats().NoopScratchPool— default no-op pool; every acquire allocates fresh. Bit-for-bit equivalent to pre-pool behavior.SizeClassedScratchPool— power-of-two slabs starting at 64 floats; scoped lifetime; per-class cap with surplus drop. Single-threaded by intent (one forward at a time per pool); concurrent forwards use separate pools.ExecutionContext.scratch: ScratchPool— backward-compatible accessor with defaultNoopScratchPool. Existing impls keep working unchanged.Why upstream
Discussed in #549. The downstream call sites (KVCache, MHA, RoPE in
SKaiNET-transformers) are transformer-specific, but the SPI itself is a generic tensor-workspace allocator. Any nn workload benefits — placing it next tosk.ainet.lang.tensor.datakeeps it reusable.Out of scope
ScratchPoolContext) — only needed where call sites don't take anExecutionContext. With the upstreamctx.scratchfield, downstream can pass it through naturally.MemorySegment-backed pool) — future iteration.Version bump
gradle.properties→0.21.0-SNAPSHOTfor downstream composite-build integration.Test plan
SizeClassedScratchPoolTest— 11 tests covering size-class rounding, scope recycling, nested scopes, stats correctness, surplus drop, no-op pool. All passing on JVM.:skainet-lang:skainet-lang-core:jvmTestpass unchanged.SKaiNET-developers/SKaiNET-transformers(composite build).🤖 Generated with Claude Code