1+ import os
12import re
3+ import json
24from pathlib import Path
35from typing import Dict
46
@@ -364,4 +366,78 @@ def detect_moe_model_type(config):
364366 'has_shared_experts' : getattr (config , 'n_shared_experts' , 0 ) > 0
365367 })
366368
367- return model_info
369+ return model_info
370+
371+
372+ def fix_qwen3_rope_config (config_dict ):
373+ """
374+ Fixed the RoPE configuration issue of the Qwen3 model
375+
376+ Args:
377+ config_dict: Configuration dictionary
378+ Returns:
379+ The fixed configuration dictionary
380+ """
381+ if 'rope_scaling' in config_dict :
382+ rope_scaling = config_dict ['rope_scaling' ]
383+
384+ if rope_scaling is None :
385+ return config_dict
386+
387+ if isinstance (rope_scaling , dict ):
388+ # Handling missing rope_type
389+ if 'rope_type' not in rope_scaling :
390+ if 'type' in rope_scaling :
391+ # If there is a 'type' field, rename it to 'rope_type'
392+ rope_scaling ['rope_type' ] = rope_scaling .pop ('type' )
393+ else :
394+ # If none, add a default rope_type
395+ rope_scaling ['rope_type' ] = 'default'
396+
397+ # Make sure the rope_scaling dictionary is structured correctly
398+ config_dict ['rope_scaling' ] = rope_scaling
399+ else :
400+ # If rope_scaling is not a dictionary, set it to None
401+ config_dict ['rope_scaling' ] = None
402+
403+ return config_dict
404+
405+
406+ def load_config_with_rope_fix (model_path ):
407+ """
408+ Safely load configurations and automatically fix RoPE configuration issues
409+ """
410+ try :
411+ # Try to load normally first
412+ config = AutoConfig .from_pretrained (model_path , trust_remote_code = True )
413+ return config
414+ except KeyError as e :
415+ if "rope_scaling" in str (e ) and "rope_type" in str (e ):
416+ print (f"RoPE configuration problem detected, trying to repair..." )
417+
418+ # Read and repair the configuration file
419+ config_path = os .path .join (model_path , "config.json" )
420+ if not os .path .exists (config_path ):
421+ raise FileNotFoundError (f"Configuration file does not exist: { config_path } " )
422+
423+ with open (config_path , 'r' , encoding = 'utf-8' ) as f :
424+ config_dict = json .load (f )
425+
426+ # Fix RoPE configuration
427+ config_dict = fix_qwen3_rope_config (config_dict )
428+
429+ # Create a config from the fixed dictionary
430+ from transformers .models .auto .configuration_auto import CONFIG_MAPPING
431+ model_type = config_dict .get ('model_type' , 'qwen3' )
432+
433+ if model_type in CONFIG_MAPPING :
434+ config_class = CONFIG_MAPPING [model_type ]
435+ config = config_class .from_dict (config_dict )
436+ else :
437+ # If the model type is not in the mapping, try the generic method
438+ config = AutoConfig .from_dict (config_dict )
439+
440+ print (f"Successfully repaired RoPE configuration" )
441+ return config
442+ else :
443+ raise e
0 commit comments