@@ -45,7 +45,7 @@ def run_quantize_with_lpcd(
4545 lpcd_config : LPCDConfig ,
4646 calibration_config : CalibrationConfig ,
4747):
48- """ Run quantization with LPCD.
48+ """Run quantization with LPCD.
4949
5050 Args:
5151 model_config (ModelConfig): Model configuration
@@ -55,8 +55,9 @@ def run_quantize_with_lpcd(
5555 calibration_config (CalibrationConfig): Calibration configuration
5656 """
5757
58- assert not (qep_config is not None and qep_config .general ), \
59- "qep_config.general must be False when qep is enabled."
58+ assert not (
59+ qep_config is not None and qep_config .general
60+ ), "qep_config.general must be False when qep is enabled."
6061
6162 # TODO: Parameterize when necessary
6263 batch_size = 16
@@ -69,7 +70,7 @@ def run_quantize_with_lpcd(
6970
7071 model_inputs = prepare_calibration_dataset (
7172 tokenizer = tokenizer ,
72- device = torch .device (' cpu' ),
73+ device = torch .device (" cpu" ),
7374 calibration_config = calibration_config ,
7475 model = model ,
7576 logger = logger ,
@@ -99,7 +100,7 @@ def run_quantize_with_lpcd(
99100 block_f = copy .deepcopy (block_q )
100101
101102 groups_q = make_grouped_module (block_q , inps_q , kwargs , device )
102-
103+
103104 # Build name→module map for block_f, then align groups_f to
104105 # groups_q by module name. Using make_grouped_module on
105106 # block_f independently can produce a different group ordering
@@ -112,12 +113,17 @@ def run_quantize_with_lpcd(
112113 name : mod for name , mod in block_q .named_modules () if isinstance (mod , nn .Linear )
113114 }
114115 groups_f = [
115- [name_to_module_f [next (n for n , m2 in name_to_module_q .items () if m2 is m )] for m in gq ]
116+ [
117+ name_to_module_f [next (n for n , m2 in name_to_module_q .items () if m2 is m )]
118+ for m in gq
119+ ]
116120 for gq in groups_q
117121 ]
118122
119123 lpcd_metrics = make_lpcd_metrics (lpcd_config , block_q , block_f )
120- lpcd_modules_q = [module for metric , _ in lpcd_metrics .metrics for _ , module in metric .named_targets ()]
124+ lpcd_modules_q = [
125+ module for metric , _ in lpcd_metrics .metrics for _ , module in metric .named_targets ()
126+ ]
121127
122128 # 3. For each group of layers, perform the following sequentially
123129 for group_q , group_f in zip (groups_q , groups_f ):
@@ -149,13 +155,10 @@ def run_quantize_with_lpcd(
149155 # Skip layers not registered for quantization
150156 if module not in quantizer .module_to_name :
151157 skipped_name = module_q_to_name .get (module , "<unknown>" )
152- logger .info (
153- "Skipping layer (not in quantization targets): %s" , skipped_name
154- )
158+ logger .info ("Skipping layer (not in quantization targets): %s" , skipped_name )
155159 continue
156160 name = quantizer .module_to_name [module ]
157161
158-
159162 # Fall back to standard quantization if the module is not LPCD targets
160163 if not module in lpcd_modules_q :
161164
@@ -190,10 +193,7 @@ def run_quantize_with_lpcd(
190193 # Update the weights of the target layer
191194 dtype = module .weight .data .dtype
192195 module .weight .data = (
193- quantizer .results [name ]
194- .compute_dequantized_weight ()
195- .to (device )
196- .to (dtype )
196+ quantizer .results [name ].compute_dequantized_weight ().to (device ).to (dtype )
197197 )
198198
199199 lpcd_metrics .mark_as_ready (module )
@@ -218,20 +218,11 @@ def run_quantize_with_lpcd(
218218 inps_f = forward_input (inps_f , block_f , kwargs , batch_size , device )
219219
220220 # DEBUG:Compute MSE between quantized and full-precision outputs
221- mse = compute_mse (
222- block_q ,
223- block_f ,
224- inps_q ,
225- inps_f ,
226- batch_size ,
227- device ,
228- kwargs
229- )
221+ mse = compute_mse (block_q , block_f , inps_q , inps_f , batch_size , device , kwargs )
230222 logger .info (f"[INFO] Layer { block_idx + 1 } MSE: { mse :.6e} " )
231223
232224 # free memory
233225 block_q .cpu ()
234226 torch .cuda .empty_cache ()
235227
236228 quantizer .execute_post_processing ()
237-
0 commit comments