1313decoded and encoded again), each output buffer filled beforehand with its own non-zero sentinel (HIP 0xA5, D3D12
14140x5A), so a byte either path leaves unwritten shows up as a difference. Output: counts only, no timing.
1515
16- Kernels: l_swin_block_c32_nc (the D3D12 port has only the `_nc` form, as Model M's ports).
16+ Kernels (the D3D12 ports have only the `_nc` forms, as Model M's ports):
17+ - l_swin_block_c32_nc: FP8 map in, FP8 out, mirror border;
18+ - l_swin_block_c32_enc_nc: Model L's encoder block, the FP16 halo map [H + 4][W + 4][32] in, FP8 out, the "first"
19+ window offsets (clean/l/FORMATS.md);
20+ - l_swin_block_c32_dec_nc: Model L's decoder block, the FP8 map in with the token-grid mirror, FP16 out, the "second"
21+ window offsets.
1722"""
1823import argparse
1924import ctypes
2934sys .path .insert (0 , ROOT )
3035
3136from clean .l .reference import oracle as L # noqa: E402
37+ from clean .l .reference import table as T # noqa: E402
3238from clean .l .runner import l_c32_check as C32 # noqa: E402
3339from clean .reference .fp8 import fp8_encode , q8 # noqa: E402
3440from clean .runner import packing as P # noqa: E402
3541
36- D3D12_KERNELS = ["l_swin_block_c32_nc" , "native_fill" ] # native_fill: the device's memset
42+ L_KERNELS = ["l_swin_block_c32_nc" , "l_swin_block_c32_enc_nc" , "l_swin_block_c32_dec_nc" ]
43+ D3D12_KERNELS = L_KERNELS + ["native_fill" ] # native_fill: the device's memset
3744# Control: Model M's swin_block_c96_nc, whose port is already proven byte for byte (clean/dx12/compare.py on whole
3845# frames): if it matches here too, the harness is sound.
3946CONTROL_KERNELS = ["swin_block_c96_nc" ]
4047CASES = C32 .CASES + [(96 , 160 , (2 , 2 )), (240 , 432 , (0 , 2 ))]
48+ # Model L's encoder / decoder blocks: grid sides multiples of 4, each with both window offsets of its kind. 4 x 8 and
49+ # 12 x 4: fewer windows than a workgroup's 8.
50+ L_BLOCK_GRIDS = C32 .L_CASES + [(96 , 160 ), (4 , 8 ), (12 , 4 )]
4151
4252
4353def prepare (build , rocm ):
@@ -79,18 +89,51 @@ def _params_c96(rng):
7989 return P .pack_stage ("enc2" , MS .random_params ("enc2" , rng ))[:P .stage_sections ("enc2" )[1 ]]
8090
8191
82- KINDS = {"l_swin_block_c32_nc" : ("l_swin.co" , 32 , _params_c32 ), "swin_block_c96_nc" : ("swin.co" , 96 , _params_c96 )}
92+ KINDS = {"l_swin_block_c32_nc" : ("l_swin.co" , 32 , _params_c32 ), "l_swin_block_c32_enc_nc" : ("l_swin.co" , 32 , _params_c32 ),
93+ "l_swin_block_c32_dec_nc" : ("l_swin.co" , 32 , _params_c32 ), "swin_block_c96_nc" : ("swin.co" , 96 , _params_c96 )}
94+
95+
96+ def _nc (blob ):
97+ return bool (np .frombuffer (blob [:4 ], np .uint32 )[0 ] & 1 )
98+
99+
100+ def block_cases (kernel , rng ):
101+ """(H, W, offset, input bytes, output bytes, blob) of every case of `kernel` whose blob has the `_nc` flag (only
102+ the _nc forms are ported); a blob without it draws no input."""
103+ gen = KINDS [kernel ][2 ]
104+ if kernel in ("l_swin_block_c32_enc_nc" , "l_swin_block_c32_dec_nc" ):
105+ enc = kernel == "l_swin_block_c32_enc_nc"
106+ kind = "first" if enc else "second"
107+ h = T .block_halo (T .TABLE , kind )
108+ assert not enc or h == 2 , "the ports read a 2-pixel halo"
109+ for (H , W ) in L_BLOCK_GRIDS :
110+ for parity in (0 , 1 ):
111+ blob = gen (rng )
112+ if not _nc (blob ):
113+ continue
114+ off = T .block_offset (T .TABLE , kind , parity )
115+ if enc :
116+ xb = rng .normal (0 , 1.5 , (H + 2 * h , W + 2 * h , 32 )).astype (np .float16 ).tobytes ()
117+ yield H , W , off , xb , H * W * 32 , blob
118+ else :
119+ yield H , W , off , fp8_encode (q8 (rng .normal (0 , 1.5 , (H , W , 32 )))).tobytes (), H * W * 64 , blob
120+ return
121+ C = KINDS [kernel ][1 ]
122+ for (H , W , off ) in CASES :
123+ blob = gen (rng )
124+ if not _nc (blob ):
125+ continue
126+ yield H , W , off , fp8_encode (q8 (rng .normal (0 , 1.5 , (H , W , C )))).tobytes (), H * W * C , blob
83127
84128# Each back end's output buffer starts filled with its own non-zero sentinel byte, different from the other's: a byte
85129# that one path (or both) never writes then differs between the two outputs instead of comparing equal as zeros.
86130HIP_SENTINEL , D3D12_SENTINEL = 0xA5 , 0x5A
87131
88132
89- def hip_block_raw (g , co , kernel , xb , blob , H , W , off , C ):
90- """The block on HIP; returns the output buffer's downloaded bytes as they are (never decoded and encoded again, so
91- for example the two FP8 NaN codes stay distinct). The output starts filled with HIP_SENTINEL."""
133+ def hip_block_raw (g , co , kernel , xb , blob , H , W , off , n ):
134+ """The block on HIP; returns the output buffer's `n` downloaded bytes as they are (never decoded and encoded again,
135+ so for example the two FP8 NaN codes stay distinct). The output starts filled with HIP_SENTINEL."""
92136 h = g .hip
93- n = H * W * C
94137 px , pw , po = h .malloc (len (xb )), h .malloc (len (blob )), h .malloc (n )
95138 try :
96139 h .upload (px , xb )
@@ -115,30 +158,25 @@ def run(build, rocm, seeds, kernels):
115158 lines , hip_out = [], {}
116159 neg = None # (case name, kernel) of the negative control
117160 for kernel in kernels :
118- co , C , gen = KINDS [kernel ]
161+ co = KINDS [kernel ][ 0 ]
119162 for si , seed in enumerate (seeds ):
120163 rng = np .random .default_rng (seed )
121- for ci , (H , W , off ) in enumerate (CASES ):
164+ for ci , (H , W , off , xb , nout , blob ) in enumerate (block_cases ( kernel , rng ) ):
122165 name = f"{ kernel } _s{ si } _{ ci } "
123- blob = gen (rng )
124- if not np .frombuffer (blob [:4 ], np .uint32 )[0 ] & 1 :
125- continue # only the _nc forms are ported
126- x = q8 (rng .normal (0 , 1.5 , (H , W , C )))
127- xb = fp8_encode (x ).tobytes ()
128- hip_out [name ] = (kernel , hip_block_raw (g , co , kernel , xb , blob , H , W , off , C ))
166+ hip_out [name ] = (kernel , hip_block_raw (g , co , kernel , xb , blob , H , W , off , nout ))
129167 _write (os .path .join (cdir , name + ".x" ), xb )
130168 _write (os .path .join (cdir , name + ".w" ), blob )
131169 ox , oy = off
132170 nwin = - (- (W + ox ) // 4 ) * - (- (H + oy ) // 4 )
133171 lines += [f"case { name } " , f"module { co } " , f"kernel { kernel } { - (- nwin // 8 )} 256" ,
134- f"arg buf { name } .x" , "arg null" , f"arg out { H * W * C } { name } .out { D3D12_SENTINEL } " , f"arg buf { name } .w" ,
172+ f"arg buf { name } .x" , "arg null" , f"arg out { nout } { name } .out { D3D12_SENTINEL } " , f"arg buf { name } .w" ,
135173 f"arg u32 { H } " , f"arg u32 { W } " , f"arg u32 { ox } " , f"arg u32 { oy } " , "" ]
136174 if neg is None and nwin > 8 :
137175 # Negative control: this case again on D3D12 with half its workgroups. The windows never processed
138176 # keep the D3D12 sentinel, so the comparison must find them: the check can fail.
139177 neg = (name , kernel )
140178 lines += [f"case negative_{ name } " , f"module { co } " , f"kernel { kernel } { max (1 , - (- nwin // 8 ) // 2 )} 256" ,
141- f"arg buf { name } .x" , "arg null" , f"arg out { H * W * C } negative_{ name } .out { D3D12_SENTINEL } " ,
179+ f"arg buf { name } .x" , "arg null" , f"arg out { nout } negative_{ name } .out { D3D12_SENTINEL } " ,
142180 f"arg buf { name } .w" , f"arg u32 { H } " , f"arg u32 { W } " , f"arg u32 { ox } " , f"arg u32 { oy } " , "" ]
143181 with open (os .path .join (cdir , "cases.txt" ), "w" , newline = "\n " ) as f :
144182 f .write ("\n " .join (lines ))
@@ -184,14 +222,15 @@ def main(argv=None):
184222 ap .add_argument ("--prepare" , action = "store_true" )
185223 ap .add_argument ("--run" , action = "store_true" )
186224 ap .add_argument ("--seeds" , type = int , default = 2 )
225+ ap .add_argument ("--kernels" , default = "," .join (L_KERNELS ))
187226 ap .add_argument ("--no-control" , action = "store_true" , help = "skip Model M's swin_block_c96_nc control" )
188227 ap .add_argument ("--json" )
189228 a = ap .parse_args (argv )
190229 os .makedirs (a .build , exist_ok = True )
191230 if a .prepare :
192231 print ("built" , prepare (a .build , a .rocm ))
193232 if a .run :
194- kernels = ["l_swin_block_c32_nc" ] + ([] if a .no_control else CONTROL_KERNELS )
233+ kernels = [k for k in a . kernels . split ( "," ) if k ] + ([] if a .no_control else CONTROL_KERNELS )
195234 res = run (a .build , a .rocm , tuple (range (89 , 89 + a .seeds )), kernels )
196235 if a .json :
197236 with open (a .json , "w" ) as f :
0 commit comments