@@ -348,6 +348,97 @@ def randn_kernel_3d(x: torch.Tensor) -> torch.Tensor:
348348 f"Slice { b_idx } std { slice_std } is not well distributed" ,
349349 )
350350
351+ def test_hl_rand_1d (self ):
352+ @helion .kernel
353+ def rand_kernel_tiled_1d (x : torch .Tensor , seed : int ) -> torch .Tensor :
354+ output = torch .zeros_like (x )
355+ (m ,) = x .shape
356+ for (tile_m ,) in hl .tile ([m ]):
357+ output [tile_m ] = hl .rand ([tile_m ], seed = seed )
358+ return output
359+
360+ x_small = torch .ones (128 , device = DEVICE )
361+ _ , output = code_and_output (rand_kernel_tiled_1d , (x_small , 42 ))
362+ _ , output2 = code_and_output (rand_kernel_tiled_1d , (x_small , 1337 ))
363+
364+ self .assertFalse (
365+ torch .allclose (output , output2 ),
366+ "Different seeds should produce different outputs" ,
367+ )
368+
369+ _ , output3 = code_and_output (rand_kernel_tiled_1d , (x_small , 42 ))
370+ self .assertTrue (
371+ torch .allclose (output , output3 ),
372+ "Same seed should produce identical outputs" ,
373+ )
374+
375+ # Check that all values are in [0, 1) range
376+ self .assertTrue (torch .all (output >= 0.0 ), "All values should be >= 0" )
377+ self .assertTrue (torch .all (output < 1.0 ), "All values should be < 1" )
378+
379+ def test_hl_rand_2d (self ):
380+ @helion .kernel
381+ def rand_kernel_tiled_2d (x : torch .Tensor , seed : int ) -> torch .Tensor :
382+ output = torch .zeros_like (x )
383+ m , n = x .shape
384+ for tile_m , tile_n in hl .tile ([m , n ]):
385+ output [tile_m , tile_n ] = hl .rand ([tile_m , tile_n ], seed = seed )
386+ return output
387+
388+ x_small = torch .ones (128 , 128 , device = DEVICE )
389+ _ , output = code_and_output (rand_kernel_tiled_2d , (x_small , 42 ))
390+ _ , output2 = code_and_output (rand_kernel_tiled_2d , (x_small , 1337 ))
391+
392+ self .assertFalse (
393+ torch .allclose (output , output2 ),
394+ "Different seeds should produce different outputs" ,
395+ )
396+
397+ _ , output3 = code_and_output (rand_kernel_tiled_2d , (x_small , 42 ))
398+ self .assertTrue (
399+ torch .allclose (output , output3 ),
400+ "Same seed should produce identical outputs" ,
401+ )
402+
403+ self .assertTrue (torch .all (output >= 0.0 ), "All values should be >= 0" )
404+ self .assertTrue (torch .all (output < 1.0 ), "All values should be < 1" )
405+
406+ def test_hl_rand_3d (self ):
407+ @helion .kernel
408+ def rand_kernel_tiled_3d (x : torch .Tensor , seed : int ) -> torch .Tensor :
409+ output = torch .zeros_like (x )
410+ b , m , n = x .shape
411+ for tile_b , tile_m , tile_n in hl .tile ([b , m , n ]):
412+ output [tile_b , tile_m , tile_n ] = hl .rand (
413+ [tile_b , tile_m , tile_n ], seed = seed
414+ )
415+ return output
416+
417+ x_small = torch .ones (16 , 32 , 64 , device = DEVICE )
418+ _ , output = code_and_output (rand_kernel_tiled_3d , (x_small , 42 ))
419+ _ , output2 = code_and_output (rand_kernel_tiled_3d , (x_small , 1337 ))
420+
421+ self .assertFalse (
422+ torch .allclose (output , output2 ),
423+ "Different seeds should produce different outputs" ,
424+ )
425+
426+ _ , output3 = code_and_output (rand_kernel_tiled_3d , (x_small , 42 ))
427+ self .assertTrue (
428+ torch .allclose (output , output3 ),
429+ "Same seed should produce identical outputs" ,
430+ )
431+
432+ self .assertTrue (torch .all (output >= 0.0 ), "All values should be >= 0" )
433+ self .assertTrue (torch .all (output < 1.0 ), "All values should be < 1" )
434+
435+ # Check distribution properties
436+ mean_val = output .mean ().item ()
437+ self .assertTrue (
438+ 0.4 < mean_val < 0.6 ,
439+ f"Mean { mean_val :.3f} should be around 0.5 for uniform distribution" ,
440+ )
441+
351442
352443if __name__ == "__main__" :
353444 unittest .main ()
0 commit comments