@@ -273,6 +273,7 @@ class TokenizerType(Enum):
273273g_tokenizer_type = TokenizerType .BPE1
274274
275275g_special_tokens : Dict = {}
276+ g_do_dequantization : bool = False
276277
277278def pad_to_len (l : list , to_len : int , v = 0 ) -> list :
278279 assert len (l ) <= to_len
@@ -605,7 +606,22 @@ def format_time(t) -> str:
605606 remain = (total - i - 1 ) * per_item
606607 print_progress_bar (i + 1 , total , prefix = desc , suffix = f"({ i } /{ total } ) { format_time (per_item )} /it rem: { format_time (remain )} " )
607608
609+ def dequantize (state_dict : dict ) -> dict :
610+ r = {}
611+ for k in state_dict .keys ():
612+ t : torch .Tensor = state_dict [k ]
613+ if k .endswith ('.weight_scale_inv' ):
614+ k = k .replace ('.weight_scale_inv' , '.weight' )
615+ assert k in state_dict
616+ r [k ] = state_dict [k ].float () * t
617+ continue
618+ if k not in r :
619+ r [k ] = t
620+ return r
621+
608622def dump_state_dict (f , weight_names , model_files , ggml_type , config , state_dict_pp , loader_fun = None ):
623+ global g_do_dequantization
624+
609625 tensor_info = []
610626 converted_names = []
611627
@@ -618,6 +634,10 @@ def dump_state_dict(f, weight_names, model_files, ggml_type, config, state_dict_
618634
619635 for state_dict in loader_fun (model_files ):
620636 this_round = []
637+
638+ if g_do_dequantization :
639+ state_dict = dequantize (state_dict )
640+
621641 state_dict = state_dict_pp (config , state_dict )
622642
623643 for x in state_dict :
@@ -8462,7 +8482,7 @@ def load_some_model(path: Path, fallback_files: list[Path] = []) -> List[Path]:
84628482 return [path ]
84638483
84648484def main ():
8465- global g_lora
8485+ global g_lora , g_do_dequantization
84668486
84678487 parser = argparse .ArgumentParser ("chatllm-convert" )
84688488 parser .add_argument ("-i" , "--model_name_or_path" , type = str )
@@ -8499,6 +8519,8 @@ def main():
84998519 else :
85008520 config = AttributeDict ({})
85018521
8522+ g_do_dequantization = config .quantization_config is not None
8523+
85028524 if arch == '' :
85038525 if config .architectures is None :
85048526 if "model_type" in config :
0 commit comments