|
16 | 16 | import argparse
|
17 | 17 | import copy
|
18 | 18 | import random
|
19 |
| -import shutil |
20 | 19 | import time
|
21 | 20 | import warnings
|
22 |
| -from pathlib import Path |
23 | 21 | from typing import Any
|
24 | 22 |
|
25 | 23 | import numpy as np
|
26 | 24 | import torch
|
27 | 25 | from accelerate.hooks import remove_hook_from_module
|
28 |
| -from example_utils import apply_kv_cache_quant, get_model, get_processor, get_tokenizer, is_enc_dec |
| 26 | +from example_utils import ( |
| 27 | + apply_kv_cache_quant, |
| 28 | + copy_custom_model_files, |
| 29 | + get_model, |
| 30 | + get_processor, |
| 31 | + get_tokenizer, |
| 32 | + is_enc_dec, |
| 33 | +) |
29 | 34 | from transformers import (
|
30 | 35 | AutoConfig,
|
31 | 36 | AutoModelForCausalLM,
|
|
85 | 90 | mto.enable_huggingface_checkpointing()
|
86 | 91 |
|
87 | 92 |
|
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 |
| - |
138 | 93 | def auto_quantize(
|
139 | 94 | model, qformat, auto_quantize_bits, calib_dataloader, calibrate_loop, batch_size=1
|
140 | 95 | ):
|
|
0 commit comments