@@ -697,6 +697,53 @@ def get_tflops(self, op, *args, **kwargs):
697697 return total_flops
698698
699699
700+ class BinaryPointwiseBenchmark (Benchmark ):
701+ """
702+ Base class for benchmarking binary pointwise operations.
703+ """
704+
705+ DEFAULT_METRICS = DEFAULT_METRICS [:] + ["tflops" ]
706+
707+ def set_more_shapes (self ):
708+ special_shapes_2d = [[1024 , 2 ** i ] for i in range (0 , 20 , 4 )]
709+ shapes_3d = [[64 , 64 , 2 ** i ] for i in range (0 , 20 , 4 )]
710+ return special_shapes_2d + shapes_3d
711+
712+ def get_input_iter (self , dtype ) -> Generator :
713+ for shape in self .shapes :
714+ inp1 = generate_tensor_input (shape , dtype , self .device )
715+ inp2 = generate_tensor_input (shape , dtype , self .device )
716+ yield inp1 , inp2
717+
718+ def get_tflops (self , op , * args , ** kwargs ):
719+ shape1 = list (args [0 ].shape )
720+ shape2 = list (args [0 ].shape )
721+ return torch .tensor (shape1 ).prod ().item () + torch .tensor (shape2 ).prod ().item ()
722+
723+
724+ class ScalarBinaryPointwiseBenchmark (Benchmark ):
725+ """
726+ Base class for benchmarking binary pointwise operations with scalar input.
727+ """
728+
729+ DEFAULT_METRICS = DEFAULT_METRICS [:] + ["tflops" ]
730+
731+ def set_more_shapes (self ):
732+ special_shapes_2d = [[1024 , 2 ** i ] for i in range (0 , 20 , 4 )]
733+ shapes_3d = [[64 , 64 , 2 ** i ] for i in range (0 , 20 , 4 )]
734+ return special_shapes_2d + shapes_3d
735+
736+ def get_input_iter (self , cur_dtype ) -> Generator :
737+ for shape in self .shapes :
738+ inp1 = 0.001 # Scalar input
739+ inp2 = generate_tensor_input (shape , cur_dtype , self .device )
740+ yield inp1 , inp2
741+
742+ def get_tflops (self , op , * args , ** kwargs ):
743+ shape = list (args [1 ].shape ) # Second argument is the tensor
744+ return torch .tensor (shape ).prod ().item ()
745+
746+
700747def generate_tensor_input (shape , dtype , device ):
701748 if dtype in FLOAT_DTYPES :
702749 return torch .randn (shape , dtype = dtype , device = device )
0 commit comments