66import torch , piquant
77torch .set_num_threads (mp .cpu_count ())
88
9- TOTAL_GIB = 4
9+ TOTAL_GIB = 32
1010ITERATIONS = 10
11- SRC_DTYPE = torch .bfloat16
12- Q_DTYPE = torch .quint2x4
13- OUT_DTYPE = torch .bfloat16
1411
15- B_PER_ELEM = torch .tensor ([], dtype = SRC_DTYPE ).element_size ()
16- TOTAL_BYTES = TOTAL_GIB * (1 << 30 )
17- N_ELEMS = TOTAL_BYTES // B_PER_ELEM
18-
19- with torch .inference_mode ():
20- x = torch .rand (N_ELEMS , dtype = SRC_DTYPE )
21- SCALE , ZP = piquant .torch .compute_quant_params (x , dtype = Q_DTYPE )
22-
23- with torch .inference_mode ():
24- q = piquant .torch .quantize (x , scale = SCALE , zero_point = int (ZP ), dtype = Q_DTYPE )
25- _ = piquant .torch .dequantize (q , scale = SCALE , zero_point = int (ZP ), dtype = OUT_DTYPE )
26-
27- quant_results , dequant_results = [], []
28- uint4_source_gib = (N_ELEMS // 2 ) / (1 << 30 )
29-
30- for i in range (ITERATIONS ):
31- t0 = time .perf_counter ()
12+ def measure_throughput (dq_type : torch .dtype , q_type : torch .dtype ):
13+ B_PER_ELEM = torch .tensor ([], dtype = dq_type ).element_size ()
14+ TOTAL_BYTES = TOTAL_GIB * (1 << 30 )
15+ N_ELEMS = TOTAL_BYTES // B_PER_ELEM
3216 with torch .inference_mode ():
33- _ = piquant .torch .quantize (x , scale = SCALE , zero_point = int (ZP ), dtype = Q_DTYPE )
34- t1 = time .perf_counter ()
35- quant_results .append (TOTAL_GIB / (t1 - t0 ))
36-
37- with torch .inference_mode ():
38- q = piquant .torch .quantize (x , scale = SCALE , zero_point = int (ZP ), dtype = Q_DTYPE )
39- t0 = time .perf_counter ()
17+ x = torch .rand (N_ELEMS , dtype = dq_type )
18+ SCALE , ZP = piquant .torch .compute_quant_params (x , dtype = q_type )
4019 with torch .inference_mode ():
41- _ = piquant .torch .dequantize (q , scale = SCALE , zero_point = int (ZP ), dtype = OUT_DTYPE )
42- t1 = time .perf_counter ()
43- dequant_results .append (uint4_source_gib / (t1 - t0 ))
44-
45- print (f"Quant bf16->uint4 avg throughput: { sum (quant_results )/ ITERATIONS :.2f} GiB/s" )
46- print (f"Dequant uint4->bf16 avg throughput: { sum (dequant_results )/ ITERATIONS :.2f} GiB/s" )
20+ q = piquant .torch .quantize (x , scale = SCALE , zero_point = int (ZP ), dtype = q_type )
21+ _ = piquant .torch .dequantize (q , scale = SCALE , zero_point = int (ZP ), dtype = dq_type )
22+ quant_results , dequant_results = [], []
23+ uint4_source_gib = (N_ELEMS // 2 ) / (1 << 30 )
24+ for i in range (ITERATIONS ):
25+ t0 = time .perf_counter ()
26+ with torch .inference_mode ():
27+ _ = piquant .torch .quantize (x , scale = SCALE , zero_point = int (ZP ), dtype = q_type )
28+ t1 = time .perf_counter ()
29+ quant_results .append (TOTAL_GIB / (t1 - t0 ))
30+ with torch .inference_mode ():
31+ q = piquant .torch .quantize (x , scale = SCALE , zero_point = int (ZP ), dtype = q_type )
32+ t0 = time .perf_counter ()
33+ with torch .inference_mode ():
34+ _ = piquant .torch .dequantize (q , scale = SCALE , zero_point = int (ZP ), dtype = dq_type )
35+ t1 = time .perf_counter ()
36+ dequant_results .append (uint4_source_gib / (t1 - t0 ))
37+
38+ print (f'Quant { dq_type } -> { q_type } avg throughput: { sum (quant_results )/ ITERATIONS :.2f} GiB/s' )
39+ print (f'Dequant { q_type } -> { dq_type } avg throughput: { sum (dequant_results )/ ITERATIONS :.2f} GiB/s' )
40+
41+ measure_throughput (torch .bfloat16 , torch .quint4x2 )
42+ measure_throughput (torch .bfloat16 , torch .quint2x4 )
0 commit comments