@@ -227,6 +227,54 @@ def model_config_from_dict(d: dict) -> ModelConfig:
227227    return  _from_dict (config_type , d )
228228
229229
230+ def  restore_original_rope_scaling (config_data : dict , original_model_path : str ) ->  dict :
231+     """Restore original rope_scaling configuration if it was modified by transformers. 
232+ 
233+     Some VLM models like Qwen2.5-VL have their rope_scaling configuration modified 
234+     by the transformers library during loading (e.g., from "mrope" to "default" with 
235+     additional fields). This function restores the original configuration. 
236+ 
237+     Args: 
238+         config_data: The model configuration dictionary to restore 
239+         original_model_path: Path to the original model directory 
240+ 
241+     Returns: 
242+         The config_data dictionary with restored rope_scaling (modified in-place) 
243+     """ 
244+     import  json 
245+     import  warnings 
246+     from  pathlib  import  Path 
247+ 
248+     try :
249+         original_config_file  =  Path (original_model_path ) /  "config.json" 
250+         if  original_config_file .exists ():
251+             with  open (original_config_file ) as  f :
252+                 raw_original_config  =  json .load (f )
253+ 
254+             # Check if rope_scaling was modified from mrope to default 
255+             if  (
256+                 "rope_scaling"  in  raw_original_config 
257+                 and  "rope_scaling"  in  config_data 
258+                 and  raw_original_config ["rope_scaling" ].get ("type" ) ==  "mrope" 
259+                 and  config_data ["rope_scaling" ].get ("type" ) ==  "default" 
260+                 and  "rope_type"  in  config_data ["rope_scaling" ]
261+             ):
262+                 print (f"Restoring original rope_scaling configuration from { original_model_path }  " )
263+                 config_data ["rope_scaling" ] =  raw_original_config ["rope_scaling" ]
264+ 
265+                 # Also restore rope_scaling in text_config if it exists 
266+                 if  (
267+                     "text_config"  in  config_data 
268+                     and  "rope_scaling"  in  config_data ["text_config" ]
269+                     and  config_data ["text_config" ]["rope_scaling" ].get ("type" ) ==  "default" 
270+                 ):
271+                     config_data ["text_config" ]["rope_scaling" ] =  raw_original_config ["rope_scaling" ]
272+     except  Exception  as  e :
273+         warnings .warn (f"Could not restore original rope_scaling configuration: { e }  " )
274+ 
275+     return  config_data 
276+ 
277+ 
230278def  pad_weights (weights , tp_size ):
231279    """Returns the padded weights to tp_size.""" 
232280    assert  len (weights .shape ) >  1 
0 commit comments