16
16
import argparse
17
17
import copy
18
18
import random
19
+ import shutil
19
20
import time
20
21
import warnings
22
+ from pathlib import Path
21
23
from typing import Any
22
24
23
25
import numpy as np
83
85
mto .enable_huggingface_checkpointing ()
84
86
85
87
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
+
86
138
def auto_quantize (
87
139
model , qformat , auto_quantize_bits , calib_dataloader , calibrate_loop , batch_size = 1
88
140
):
@@ -603,6 +655,9 @@ def output_decode(generated_ids, input_shape):
603
655
inference_tensor_parallel = args .inference_tensor_parallel ,
604
656
inference_pipeline_parallel = args .inference_pipeline_parallel ,
605
657
)
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 )
606
661
else :
607
662
# Check arguments for unified_hf export format and set to default if unsupported arguments are provided
608
663
assert args .sparsity_fmt == "dense" , (
@@ -620,6 +675,9 @@ def output_decode(generated_ids, input_shape):
620
675
export_dir = export_path ,
621
676
)
622
677
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
+
623
681
# Restore default padding and export the tokenizer as well.
624
682
if tokenizer is not None :
625
683
tokenizer .padding_side = default_padding_side
0 commit comments