@@ -285,3 +285,74 @@ def _launch_big(x: fx.Tensor, stream: fx.Stream = fx.Stream(None)):
285285
286286 source_ir = _get_source_ir (_launch_big , self .x )
287287 assert "known_block_size = array<i32: 512, 1, 1>" in source_ir
288+
289+
290+ class TestKnownBlockSizeTraceAccessor :
291+ """Verify the trace-time ``fx.known_block_size()`` view of the same value."""
292+
293+ @pytest .fixture (autouse = True )
294+ def _setup (self ):
295+ self .x = torch .zeros (64 , device = "cuda" , dtype = torch .float32 )
296+
297+ def test_raises_outside_a_kernel (self ):
298+ with pytest .raises (RuntimeError , match = "no compile-time block size" ):
299+ fx .known_block_size ()
300+
301+ def test_sees_the_declared_size (self ):
302+ seen = []
303+
304+ @flyc .kernel (known_block_size = [128 , 4 , 2 ])
305+ def _kn (x : fx .Tensor ):
306+ seen .append (fx .known_block_size ())
307+
308+ @flyc .jit
309+ def _launch (x : fx .Tensor , stream : fx .Stream = fx .Stream (None )):
310+ _kn (x ).launch (grid = (1 , 1 , 1 ), block = (128 , 4 , 2 ), stream = stream )
311+
312+ _get_source_ir (_launch , self .x )
313+ assert seen == [(128 , 4 , 2 )]
314+
315+ def test_sees_the_size_inferred_from_static_launch_dims (self ):
316+ seen = []
317+
318+ @flyc .kernel
319+ def _kn (x : fx .Tensor ):
320+ seen .append (fx .known_block_size ())
321+
322+ @flyc .jit
323+ def _launch (x : fx .Tensor , stream : fx .Stream = fx .Stream (None )):
324+ _kn (x ).launch (grid = (1 , 1 , 1 ), block = (256 , 1 , 1 ), stream = stream )
325+
326+ _get_source_ir (_launch , self .x )
327+ assert seen == [(256 , 1 , 1 )]
328+
329+ def test_raises_for_a_dynamic_launch (self ):
330+ @flyc .kernel
331+ def _kn (x : fx .Tensor , nthreads : fx .Int32 ):
332+ fx .known_block_size ()
333+
334+ @flyc .jit
335+ def _launch (x : fx .Tensor , nthreads : fx .Int32 ):
336+ _kn (x , nthreads ).launch (grid = (1 , 1 , 1 ), block = (nthreads , 1 , 1 ))
337+
338+ with pytest .raises (RuntimeError , match = "no compile-time block size" ):
339+ _launch (self .x , 64 )
340+
341+ def test_is_visible_from_a_nested_jit_helper (self ):
342+ """An inner tracing frame carries no size of its own and inherits the kernel's."""
343+ seen = []
344+
345+ @flyc .jit
346+ def _helper ():
347+ seen .append (fx .known_block_size ())
348+
349+ @flyc .kernel (known_block_size = [64 , 1 , 1 ])
350+ def _kn (x : fx .Tensor ):
351+ _helper ()
352+
353+ @flyc .jit
354+ def _launch (x : fx .Tensor , stream : fx .Stream = fx .Stream (None )):
355+ _kn (x ).launch (grid = (1 , 1 , 1 ), block = (64 , 1 , 1 ), stream = stream )
356+
357+ _get_source_ir (_launch , self .x )
358+ assert seen == [(64 , 1 , 1 )]
0 commit comments