1616import argparse
1717import copy
1818import random
19+ import shutil
1920import time
2021import warnings
22+ from pathlib import Path
2123from typing import Any
2224
2325import numpy as np
8385mto .enable_huggingface_checkpointing ()
8486
8587
88+ def copy_custom_model_files (source_path : str , export_path : str , trust_remote_code : bool = False ):
89+ """Copy custom model files (configuration_*.py, modeling_*.py, etc.) from source to export directory.
90+
91+ Args:
92+ source_path: Path to the original model directory
93+ export_path: Path to the exported model directory
94+ trust_remote_code: Whether trust_remote_code was used (only copy files if True)
95+ """
96+ if not trust_remote_code :
97+ return
98+
99+ source_dir = Path (source_path )
100+ export_dir = Path (export_path )
101+
102+ if not source_dir .exists ():
103+ print (f"Warning: Source directory { source_path } does not exist" )
104+ return
105+
106+ if not export_dir .exists ():
107+ print (f"Warning: Export directory { export_path } does not exist" )
108+ return
109+
110+ # Common patterns for custom model files that need to be copied
111+ custom_file_patterns = [
112+ "configuration_*.py" ,
113+ "modeling_*.py" ,
114+ "tokenization_*.py" ,
115+ "processing_*.py" ,
116+ "image_processing_*.py" ,
117+ "feature_extraction_*.py" ,
118+ ]
119+
120+ copied_files = []
121+ for pattern in custom_file_patterns :
122+ for file_path in source_dir .glob (pattern ):
123+ if file_path .is_file ():
124+ dest_path = export_dir / file_path .name
125+ try :
126+ shutil .copy2 (file_path , dest_path )
127+ copied_files .append (file_path .name )
128+ print (f"Copied custom model file: { file_path .name } " )
129+ except Exception as e :
130+ print (f"Warning: Failed to copy { file_path .name } : { e } " )
131+
132+ if copied_files :
133+ print (f"Successfully copied { len (copied_files )} custom model files to { export_path } " )
134+ else :
135+ print ("No custom model files found to copy" )
136+
137+
86138def auto_quantize (
87139 model , qformat , auto_quantize_bits , calib_dataloader , calibrate_loop , batch_size = 1
88140):
@@ -603,6 +655,9 @@ def output_decode(generated_ids, input_shape):
603655 inference_tensor_parallel = args .inference_tensor_parallel ,
604656 inference_pipeline_parallel = args .inference_pipeline_parallel ,
605657 )
658+
659+ # Copy custom model files for TensorRT-LLM export as well
660+ copy_custom_model_files (args .pyt_ckpt_path , export_path , args .trust_remote_code )
606661 else :
607662 # Check arguments for unified_hf export format and set to default if unsupported arguments are provided
608663 assert args .sparsity_fmt == "dense" , (
@@ -620,6 +675,9 @@ def output_decode(generated_ids, input_shape):
620675 export_dir = export_path ,
621676 )
622677
678+ # Copy custom model files (configuration_*.py, modeling_*.py, etc.) if trust_remote_code is used
679+ copy_custom_model_files (args .pyt_ckpt_path , export_path , args .trust_remote_code )
680+
623681 # Restore default padding and export the tokenizer as well.
624682 if tokenizer is not None :
625683 tokenizer .padding_side = default_padding_side
0 commit comments