Altifigence Academy

9 / 24 · Concept

Direct convolution reference

For inputs [2,3], weights [4,−1] and bias one, what wrong result occurs if bias is added once per input-channel iteration?

Prefer an inspectable reference

A golden model prioritizes a visible contract over speed. Iterate output channel, output row and output column; then reduce input channel and kernel row and column. Calculate each input coordinate and supply zero outside the bounds. This exposes the first incorrect output and the exact term that diverges. It need not mimic the hardware's optimized loop order.

y[m,h,w]=b[m]+∑c∑r∑sx[c,hSh−Pt+rDh,wSw−Pl+sDw]w[m,c,r,s]y[m,h,w]=b[m]+\sum_c\sum_r\sum_s x[c,hS_h-P_t+rD_h,wS_w-P_l+sD_w]w[m,c,r,s]

Initialize the accumulator with b[m] once per output. Adding it inside the reduction would repeat the bias. In the integer model, x and w are centered codes and bias uses the product scale.

Worked multichannel example

Use two 1×1 input channels holding two and three. Output channel zero has weights [4,−1] and bias one, giving 1+8−3=6. Output channel one has weights [−2,5] and bias −1, giving −1−4+15=10. A shared accumulator must start the second output independently rather than retaining the first result.

For input [1,2,3;4,5,6;7,8,9] and kernel [1,2;3,4], expect [37,47;67,77]. The first logged products are 1,4,12,20. The next are 2,6,15,24. Inspect a hand-calculated position; final values alone can conceal compensating errors.

Compare the direct model against a separately structured patch-and-dot model. Sharing the same address routine in both can share the same bug. Combine independent structure with hand fixtures. Keep randomized seeds reproducible and retain a failing tensor. Compare both the unconverted accumulator and the final output code to localize numerical failures.

Our model.mjs accepts X[C][H][W] and W[M][C/G][R][S], with one implicit batch. It checks rectangular tensors, channel compatibility and groups. It does not import framework models. Use distinct values, negatives and asymmetric weights rather than an all-zero fixture. Compare the conventions with ONNX Conv while documenting this teaching model's limits.

Try it yourself

For inputs [2,3], weights [4,−1] and bias one, what wrong result occurs if bias is added once per input-channel iteration?

Read the explanation

The correct result is 1+8−3=6. Repeating bias twice gives 8−3+2=7. Initialize bias outside the channel reduction.

Your choice applies to this browser. Change it any time using the footer.