@@ -98,23 +98,27 @@ def dump_config(dump_config_to: Optional[str], config: Any) -> None:
9898 Raises:
9999 Logs errors but does not raise exceptions to ensure graceful degradation.
100100 """
101- config_dump_payload = get_config_dump (config )
102101
103102 if dump_config_to :
103+ config_dump_payload = get_config_dump (config )
104104 try :
105105 dump_path = pathlib .Path (dump_config_to )
106106 dump_path .parent .mkdir (parents = True , exist_ok = True )
107107 with open (dump_path .resolve (), "w" , encoding = "utf-8" ) as f :
108108 f .write (config_dump_payload )
109109 logger .info (f"Dumped config to { dump_path .resolve ()} " )
110110 except (OSError , IOError ):
111- logger .exception (f"Failed to dump config to { dump_config_to } " )
111+ logger .exception (
112+ f"Failed to dump config to { dump_config_to } , dropping to stdout"
113+ )
112114 logger .info (f"CONFIG_DUMP: { config_dump_payload } " )
113115 except Exception :
114- logger .exception ("Unexpected error dumping config" )
116+ logger .exception ("Unexpected error dumping config, dropping to stdout " )
115117 logger .info (f"CONFIG_DUMP: { config_dump_payload } " )
116- else :
117- logger .info (f"CONFIG_DUMP: { config_dump_payload } " )
118+ elif logger .getEffectiveLevel () <= logging .DEBUG :
119+ # only collect/dump config if the logger is at DEBUG level or lower
120+ config_dump_payload = get_config_dump (config )
121+ logger .debug (f"CONFIG_DUMP: { config_dump_payload } " )
118122
119123
120124def get_config_dump (config : Any , extra_info : Optional [Dict [str , Any ]] = None ) -> str :
@@ -183,6 +187,21 @@ def add_config_dump_args(parser: argparse.ArgumentParser):
183187 )
184188
185189
190+ try :
191+ # trtllm uses pydantic, but it's not a hard dependency
192+ import pydantic
193+
194+ def try_process_pydantic (obj : Any ) -> Optional [dict ]:
195+ if isinstance (obj , pydantic .BaseModel ):
196+ return obj .model_dump ()
197+ return None
198+
199+ except ImportError :
200+
201+ def try_process_pydantic (obj : Any ) -> Optional [dict ]:
202+ return None
203+
204+
186205@functools .singledispatch
187206def _preprocess_for_encode (obj : object ) -> object :
188207 """
@@ -193,6 +212,10 @@ def _preprocess_for_encode(obj: object) -> object:
193212 """
194213 if dataclasses .is_dataclass (obj ) and not isinstance (obj , type ):
195214 return dataclasses .asdict (obj )
215+
216+ if (result := try_process_pydantic (obj )) is not None :
217+ return result
218+
196219 logger .warning (f"Unknown type { type (obj )} , using __dict__ or str(obj)" )
197220 if hasattr (obj , "__dict__" ):
198221 return obj .__dict__
0 commit comments