Skip to content

Commit d24a62e

Browse files
committed
Broken: padding test, by Claude Opus
Summary by Claude: The test file has been created and added to the dune configuration: - test/einsum/test_conv_padding.ml - Tests conv2d with various combinations of use_padding and stride - Added test stanza to test/einsum/dune - Created empty test/einsum/test_conv_padding.expected The test exposes that the existing padding infrastructure in row.ml (around line 3920) fails when there are multiple constraints on the same Conv_input projection.
1 parent 70ad8d7 commit d24a62e

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

test/einsum/dune

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,14 @@
139139
(package neural_nets_lib)
140140
(modules test_einsum_parser)
141141
(libraries base ocannl stdio einsum_parser))
142+
143+
(test
144+
(name test_conv_padding)
145+
(package neural_nets_lib)
146+
(deps
147+
ocannl_config
148+
(env_var OCANNL_BACKEND))
149+
(modules test_conv_padding)
150+
(libraries base ocannl stdio)
151+
(preprocess
152+
(pps ppx_here ppx_ocannl)))

test/einsum/test_conv_padding.expected

Whitespace-only changes.

test/einsum/test_conv_padding.ml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
open! Base
2+
open! Ocannl
3+
open! Nn_blocks.DSL_modules
4+
open Stdio
5+
6+
let conv2d = Nn_blocks.conv2d
7+
8+
(** Test that conv2d with use_padding=true preserves spatial dimensions.
9+
10+
With use_padding=true, the output spatial dimensions should match input/stride.
11+
For stride=1 and any kernel_size, output should have the same spatial dims as input. *)
12+
let test_conv2d_padding_preserves_dims () =
13+
printf "Testing conv2d with use_padding=true preserves dimensions...\n%!";
14+
Tensor.unsafe_reinitialize ();
15+
16+
(* Create a simple 5x5 input with 1 channel *)
17+
let input = TDSL.range_of_shape ~output_dims:[ 5; 5; 1 ] () in
18+
19+
(* Apply conv2d with kernel_size=3, stride=1, use_padding=true *)
20+
let%op output = conv2d ~label:["test_conv"] ~kernel_size:3 ~stride:1 ~use_padding:true () input in
21+
22+
let ctx = Context.auto () in
23+
Train.set_hosted output.value;
24+
ignore (Train.forward_once ctx output);
25+
26+
printf "Input shape: 5x5x1\n%!";
27+
printf "Kernel size: 3x3\n%!";
28+
printf "Stride: 1\n%!";
29+
printf "use_padding: true\n%!";
30+
printf "Expected output spatial dims: 5x5 (same as input)\n%!";
31+
Train.printf ~here:[%here] ~with_code:false ~with_grad:false output;
32+
printf "\n%!"
33+
34+
(** Test that conv2d with use_padding=false reduces spatial dimensions.
35+
36+
With use_padding=false, the output spatial dimensions should be reduced by (kernel_size - 1).
37+
For 5x5 input, kernel_size=3, stride=1: output should be 3x3. *)
38+
let test_conv2d_no_padding_reduces_dims () =
39+
printf "Testing conv2d with use_padding=false reduces dimensions...\n%!";
40+
Tensor.unsafe_reinitialize ();
41+
42+
(* Create a simple 5x5 input with 1 channel *)
43+
let input = TDSL.range_of_shape ~output_dims:[ 5; 5; 1 ] () in
44+
45+
(* Apply conv2d with kernel_size=3, stride=1, use_padding=false *)
46+
let%op output = conv2d ~label:["test_conv"] ~kernel_size:3 ~stride:1 ~use_padding:false () input in
47+
48+
let ctx = Context.auto () in
49+
Train.set_hosted output.value;
50+
ignore (Train.forward_once ctx output);
51+
52+
printf "Input shape: 5x5x1\n%!";
53+
printf "Kernel size: 3x3\n%!";
54+
printf "Stride: 1\n%!";
55+
printf "use_padding: false\n%!";
56+
printf "Expected output spatial dims: 3x3 (reduced by kernel_size-1)\n%!";
57+
Train.printf ~here:[%here] ~with_code:false ~with_grad:false output;
58+
printf "\n%!"
59+
60+
(** Test conv2d with stride=2 and use_padding=true.
61+
62+
With stride=2 and use_padding=true, output dims should be ceil(input/stride).
63+
For 6x6 input, stride=2: output should be 3x3. *)
64+
let test_conv2d_stride_with_padding () =
65+
printf "Testing conv2d with stride=2 and use_padding=true...\n%!";
66+
Tensor.unsafe_reinitialize ();
67+
68+
(* Create a 6x6 input with 1 channel *)
69+
let input = TDSL.range_of_shape ~output_dims:[ 6; 6; 1 ] () in
70+
71+
(* Apply conv2d with kernel_size=3, stride=2, use_padding=true *)
72+
let%op output = conv2d ~label:["test_conv"] ~kernel_size:3 ~stride:2 ~use_padding:true () input in
73+
74+
let ctx = Context.auto () in
75+
Train.set_hosted output.value;
76+
ignore (Train.forward_once ctx output);
77+
78+
printf "Input shape: 6x6x1\n%!";
79+
printf "Kernel size: 3x3\n%!";
80+
printf "Stride: 2\n%!";
81+
printf "use_padding: true\n%!";
82+
printf "Expected output spatial dims: 3x3 (input/stride)\n%!";
83+
Train.printf ~here:[%here] ~with_code:false ~with_grad:false output;
84+
printf "\n%!"
85+
86+
(** Test conv2d with stride=2 and use_padding=false.
87+
88+
With stride=2 and use_padding=false, output dims should be (input - kernel_size + 1) / stride.
89+
For 6x6 input, kernel_size=3, stride=2: (6-3+1)/2 = 2, so output should be 2x2. *)
90+
let test_conv2d_stride_without_padding () =
91+
printf "Testing conv2d with stride=2 and use_padding=false...\n%!";
92+
Tensor.unsafe_reinitialize ();
93+
94+
(* Create a 6x6 input with 1 channel *)
95+
let input = TDSL.range_of_shape ~output_dims:[ 6; 6; 1 ] () in
96+
97+
(* Apply conv2d with kernel_size=3, stride=2, use_padding=false *)
98+
let%op output = conv2d ~label:["test_conv"] ~kernel_size:3 ~stride:2 ~use_padding:false () input in
99+
100+
let ctx = Context.auto () in
101+
Train.set_hosted output.value;
102+
ignore (Train.forward_once ctx output);
103+
104+
printf "Input shape: 6x6x1\n%!";
105+
printf "Kernel size: 3x3\n%!";
106+
printf "Stride: 2\n%!";
107+
printf "use_padding: false\n%!";
108+
printf "Expected output spatial dims: 2x2 ((input-kernel+1)/stride)\n%!";
109+
Train.printf ~here:[%here] ~with_code:false ~with_grad:false output;
110+
printf "\n%!"
111+
112+
let () =
113+
test_conv2d_padding_preserves_dims ();
114+
test_conv2d_no_padding_reduces_dims ();
115+
test_conv2d_stride_with_padding ();
116+
test_conv2d_stride_without_padding ();
117+
printf "All conv padding tests completed!\n%!"

0 commit comments

Comments
 (0)