@@ -580,6 +580,67 @@ def get_input_iter(self, cur_dtype) -> Generator:
580580 yield inp ,
581581
582582
583+ class TexGluBenchmark (Benchmark ):
584+ DEFAULT_METRICS = DEFAULT_METRICS [:] + ["tflops" ]
585+ # Triton grid_y is capped at 65535, BLOCK_SIZE_H=64 -> last dim <= 8388480.
586+ MAX_LAST_DIM = 2 * 64 * 65535
587+
588+ def set_more_shapes (self ):
589+ # Last dim must be even for GLU operations to split
590+ special_shapes_2d = [[1024 , 2 ** i ] for i in range (1 , 20 , 4 )]
591+ sp_shapes_3d = [[64 , 64 , 2 ** i ] for i in range (1 , 15 , 4 )]
592+
593+ return special_shapes_2d + sp_shapes_3d
594+
595+ def init_user_config (self ):
596+ super ().init_user_config ()
597+ supported = []
598+ for shape in self .shapes :
599+ last_dim = shape [- 1 ]
600+ if last_dim % 2 != 0 :
601+ continue
602+ if last_dim > self .MAX_LAST_DIM :
603+ continue
604+ supported .append (shape )
605+ if not supported :
606+ pytest .skip (
607+ "No geglu shapes satisfy the constraints of FlagGems implementation."
608+ )
609+ self .shapes = supported
610+
611+
612+ class TexGluForwardBenchmark (TexGluBenchmark ):
613+ def get_input_iter (self , dtype ):
614+ for shape in self .shapes :
615+ x = generate_tensor_input (shape , dtype , self .device )
616+ # TE GLU APIs typically accept (input, quantizer).
617+ yield (x , None )
618+
619+ def get_tflops (self , op , * args , ** kwargs ):
620+ # args[0] is the input tensor x
621+ shape = list (args [0 ].shape )
622+ return torch .tensor (shape ).prod ().item ()
623+
624+
625+ class TexGluBackwardBenchmark (TexGluBenchmark ):
626+ def get_input_iter (self , dtype ):
627+ for shape in self .shapes :
628+ inp = generate_tensor_input (shape , dtype , self .device )
629+
630+ out_shape = list (shape )
631+ out_shape [- 1 ] = out_shape [- 1 ] // 2
632+
633+ grad_out = torch .randn (out_shape , dtype = dtype , device = self .device )
634+
635+ yield grad_out , inp , None
636+
637+ def get_tflops (self , op , * args , ** kwargs ):
638+ # args[1] is the original input tensor 'inp'
639+ inp_shape = list (args [1 ].shape )
640+ # Proxy FLOPs estimate: forward + backward cost roughly approximated
641+ return torch .tensor (inp_shape ).prod ().item () * 2
642+
643+
583644def generate_tensor_input (shape , dtype , device ):
584645 if dtype in FLOAT_DTYPES :
585646 return torch .randn (shape , dtype = dtype , device = device )
0 commit comments