Track per-sample shape through DSL spatial layers (#535)#538
Merged
michalharakal merged 1 commit intodevelopfrom Apr 21, 2026
Merged
Track per-sample shape through DSL spatial layers (#535)#538michalharakal merged 1 commit intodevelopfrom
michalharakal merged 1 commit intodevelopfrom
Conversation
The DSL's flatten() previously fell back to a hardcoded lastDimension = 1568 (the value that happens to fit the MNIST CNN reference model). Any other architecture - e.g. a 64-channel CNN over 32x32 inputs - hit ArrayIndexOutOfBounds in the following dense layer. Add a per-sample shape tracker (currentShape: IntArray?) to StageImpl and NeuralNetworkDslImpl, plus a new input(intArrayOf(...)) overload that seeds it. conv1d/2d/3d, maxPool2d, avgPool2d, and upsample2d now update currentShape using the same arithmetic as VoidTensorOps via ConvShapeUtils (extended with pool2d and upsample2d helpers, building on the helper introduced in #537). flatten() reads currentShape and honors startDim / endDim instead of guessing 1568. When no input shape is declared we leave lastDimension untouched so existing flatten-only runtime tests keep working - dense() will surface the gap with a clear error if it actually matters. Update MnistCnn to declare input(intArrayOf(1, 28, 28)) so it works under the new shape inference instead of relying on the magic constant. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
michalharakal
added a commit
that referenced
this pull request
Apr 21, 2026
Bump VERSION_NAME to 0.19.0 in the root gradle.properties, expand the CHANGELOG [0.19.0] - 2026-04-20 section to cover the full 130 commits since 0.18.0 — not just the tokenizer work but the StableHLO → IREE lowering pipeline (softmax/layerNorm/rmsnorm real lowerings, gather/ embedding/concat/slice/cast converters, ConstantMaterializationPolicy, dense<v> splat folding, SSA type tracking), the new skainet-io-iree- params IrpaWriter, skainet-backend-api module, Antora docs migration with Diátaxis layout, Java API polish (#400), androidNativeArm32 target, and the graph/DSL shape-inference fixes (#535, #536, #537, #538) that unblock non-MNIST CNN architectures and Whisper-encoder HLO compilation. Refresh the README install snippet and "What's New" section to reflect the 0.19.0 highlights, and note the tokenizer milestone on the Q2 2026 roadmap line. Ops docs regenerated so the stamped version matches the new VERSION_NAME. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Merged
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 #535.
Summary
currentShape: IntArray?tracker toStageImplandNeuralNetworkDslImplsoflatten()knows the real spatial shape instead of guessing.input(inputShape: IntArray, ...)overload — required to seed the tracker for any CNN architecture.conv1d/2d/3d,maxPool2d,avgPool2d, andupsample2dto updatecurrentShapeusingConvShapeUtils(same single-source-of-truth helper introduced in Compute real conv output shapes in graph operations (#536) #537, extended here withpool2dOutputShapeandupsample2dOutputShape).flatten()computelastDimensionfromcurrentShapehonoringstartDim/endDim. The hardcoded1568fallback is gone. When no input shape is declared,lastDimensionis left untouched so existing flatten-only runtime tests keep building; a downstreamdense()will surface the gap.Conv*layers now seedinChannelsfrom the tracked input shape (when rank matches), so the user no longer has to repeatinChannels = Nafter declaring a multi-dim input.MnistCnnto declareinput(intArrayOf(1, 28, 28))since the magic constant is gone.Why now
This builds on the conv shape-inference work from #530, #532, and #537. With those three landing the eager + graph paths for conv shape math,
flatten()was the last shape-inference gap visible from the DSL. Issue #535 reported it crashing custom architectures withArrayIndexOutOfBoundsException.Test plan
./gradlew :skainet-lang:skainet-lang-core:jvmTest(incl. newCnnShapeInferenceTest, 8 cases — MNIST, custom 64-channel CNN, conv1d Whisper-style, upsample, avgPool, stage propagation, backward-compat for bareflatten)./gradlew :skainet-lang:skainet-lang-models:jvmTest(MNIST CNN model now usesinput(intArrayOf(1, 28, 28)))./gradlew :skainet-compile:skainet-compile-core:jvmTest./gradlew :skainet-compile:skainet-compile-hlo:jvmTestArrayIndexOutOfBoundsException🤖 Generated with Claude Code