Repository navigation
spec(conv2d_layer): forward slides its kernel; scalar conv_at for the nn-conv widget (Closes #10612) - #10744
Merged
Conversation
… nn-conv widget (Closes #10612) forward multiplied one kernel-sized patch elementwise by the weights, so the 2x2 input with the 2x2 kernel gave four numbers where a convolution gives one (3.25), and its test checked the four. No fn lowered to WASM, so course 10 lesson 10 on t27.ai (trinity widgets/nn-conv) falls back to average pooling. - forward: 2-D cross-correlation over one H x W channel with stride and zero padding, side (n + 2p - k) / s + 1, bias once per output cell. backward: the sliding gradient; the bias gradient is its return value. - Scalar fns, lowered by 999 scripts/t27-logic.mjs (5 fns): out_size, conv_at (fixed 5x5 square image, identity / Sobel x / Sobel y / box blur), kernel_at, image_at, default_at (the 2x2 case, 3.25 in node). - VERSION 2 with a dated header note, and a test that pins it. - test-report 14 of 14 pass, 0 vacuous; every row of the issue's table has a test; a 3x3 input with the 2x2 kernel pins the slide. - 9 negative-control mutants (fallback, stride, padding, flip, bias, and four in backward), 9 killed. - Site compiler: typecheck ok, all seven backends. dupes --bodies: no group in this spec (three unused duplicate helpers removed). - Seals saved; NOW entry in docs/now/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ScHVrGSr6zZUdwkdC8DR9k
Contributor
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 #10612. Part of EPIC #9063 (the course factory), course 10 Neural networks in t27.
Why
specs/ml/layers/conv2d_layer.t27did not slide a kernel.forwardmultiplied one kernel-sized patch elementwise by the weights and added the bias. On the 2x2 input with the 2x2 kernel, a convolution gives one number, 11 + 20.5 + 30.25 + 40.1 + 0.1 = 3.25. The spec gave four, and its test checked those four. None of its fns lowered to WASM either. Because of that, course 10 lesson 10 on t27.ai (trinityapps/website/public/widgets/nn-conv/) uses average pooling (pool4Atfromavgpool2d_layer.t27) instead of a convolution until this lands.What changed (one spec, its two seals, one NOW entry)
forwardis now the deep-learning convolution: a 2-D cross-correlation, so the kernel is not flipped. It runs over one H x W channel, row-major, with a stride and zero padding. The output side is(n + 2p - k) / s + 1, and the bias is added once per output cell. It takesheight, width. The per-cell sum lives inconv_cell, whichforwardandconv_atshare.backwardis the sliding gradient:grad_inputandgrad_weightsare accumulated over every window. The bias gradient is now its return value instead of a 1-element slice. That keeps it at 8 parameters, so the site compiler no longer prints its "9 parameters" warning.scripts/t27-logic.mjslowers them all. It reports5 fns lowered:outSize,convAt,kernelAt,imageAt,defaultAt.out_size(n, kernel, stride, padding)returns 0 when the kernel does not fit or when the stride or the kernel is 0.conv_at(kernel_id, stride, padding, index)convolves the fixed 5x5 image (a bright 3x3 square) with kernel 0 identity, 1 Sobel x, 2 Sobel y or 3 box blur. It returns 0.0 past the end.kernel_at(kernel_id, index)andimage_at(index)return the same numbers the spec convolves, so the widget can draw them.default_at(index)is the old 2x2 case. It is one cell, 3.25.default_*helpers had one identical body (at27c dupes --bodiesgroup). They are removed.Evidence
The t27c bootstrap has not changed since 239b4b0. Zig is 0.16.0.
t27c test-report --verbose specs/ml/layers/conv2d_layer.t27: 14 tests, 14 pass, FAIL 0, 0 vacuous, and 5 invariants proved.Negative controls. Each mutant was applied,
test-reportwas run, and the spec was restored. All 9 were killed:has_biasignoredLowering. I ran
node scripts/t27-logic.mjs <t27> ml/layers/conv2d_layerand then ran the compiled WASM in node:defaultAt(0)= 3.25outSize(5,3,1,0)= 3 andoutSize(5,3,2,1)= 3convAt(1,1,0,3)= 4,convAt(1,1,0,5)= -4 andconvAt(1,2,1,3)= 4Site compiler (trinity
public/t27/t27_compiler.wasm): typecheck ok, 0 errors, 0 discarded, HIR ok, and all seven backends print (c, js, rust, ts, verilog, verilog_hir, zig).Local equivalents of the required checks:
check_allandcheck_budgetboth exit 0.check_specs_still_parse.pyreports 0 newly unparseable.dupe_scan.pyreports no new duplicate body and no known group grew.t27c dupes --bodiesnames no group in this spec.check_json_parses.pyOK.check_now_entry_shape.pyOK.No spec in
specs/usesConv2d, so there are no consumers to re-run.t27c seal --saverefreshed bothlayers_Conv2d.jsonandConv2d.json, which name the same spec.Not in this PR: re-vendoring the spec into trinity and switching
nn-conv/tool.jsfrompool4AttoconvAt.Generated by Claude Code