-
Notifications
You must be signed in to change notification settings - Fork 169
[Bugfix] Enable essential python files export in llm_ptq examples by adding a customized copy function #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
39ccfad
3b0f98a
4aa4285
9d305fa
21fa47f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -14,8 +14,10 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
# limitations under the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
import os | ||||||||||||||||||||||||||||||||||||||||||||||||||
import shutil | ||||||||||||||||||||||||||||||||||||||||||||||||||
import sys | ||||||||||||||||||||||||||||||||||||||||||||||||||
import warnings | ||||||||||||||||||||||||||||||||||||||||||||||||||
from pathlib import Path | ||||||||||||||||||||||||||||||||||||||||||||||||||
from typing import Any | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
import torch | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -263,3 +265,53 @@ def apply_kv_cache_quant(quant_cfg: dict[str, Any], kv_cache_quant_cfg: dict[str | |||||||||||||||||||||||||||||||||||||||||||||||||
quant_cfg["algorithm"] = "max" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return quant_cfg | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
def copy_custom_model_files(source_path: str, export_path: str, trust_remote_code: bool = False): | ||||||||||||||||||||||||||||||||||||||||||||||||||
"""Copy custom model files (configuration_*.py, modeling_*.py, etc.) from source to export directory. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||
source_path: Path to the original model directory | ||||||||||||||||||||||||||||||||||||||||||||||||||
export_path: Path to the exported model directory | ||||||||||||||||||||||||||||||||||||||||||||||||||
trust_remote_code: Whether trust_remote_code was used (only copy files if True) | ||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||
if not trust_remote_code: | ||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
source_dir = Path(source_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||
export_dir = Path(export_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
if not source_dir.exists(): | ||||||||||||||||||||||||||||||||||||||||||||||||||
print(f"Warning: Source directory {source_path} does not exist") | ||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
if not export_dir.exists(): | ||||||||||||||||||||||||||||||||||||||||||||||||||
print(f"Warning: Export directory {export_path} does not exist") | ||||||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
# Common patterns for custom model files that need to be copied | ||||||||||||||||||||||||||||||||||||||||||||||||||
custom_file_patterns = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||
"configuration_*.py", | ||||||||||||||||||||||||||||||||||||||||||||||||||
"modeling_*.py", | ||||||||||||||||||||||||||||||||||||||||||||||||||
"tokenization_*.py", | ||||||||||||||||||||||||||||||||||||||||||||||||||
"processing_*.py", | ||||||||||||||||||||||||||||||||||||||||||||||||||
"image_processing_*.py", | ||||||||||||||||||||||||||||||||||||||||||||||||||
"feature_extraction_*.py", | ||||||||||||||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
copied_files = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||
for pattern in custom_file_patterns: | ||||||||||||||||||||||||||||||||||||||||||||||||||
for file_path in source_dir.glob(pattern): | ||||||||||||||||||||||||||||||||||||||||||||||||||
if file_path.is_file(): | ||||||||||||||||||||||||||||||||||||||||||||||||||
dest_path = export_dir / file_path.name | ||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||
shutil.copy2(file_path, dest_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||
copied_files.append(file_path.name) | ||||||||||||||||||||||||||||||||||||||||||||||||||
print(f"Copied custom model file: {file_path.name}") | ||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||
print(f"Warning: Failed to copy {file_path.name}: {e}") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
393
to
407
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Preserve directory structure and recurse; current code may miss files and break imports
Apply this diff to recurse and keep relative paths: - for pattern in custom_file_patterns:
- for file_path in source_dir.glob(pattern):
- if file_path.is_file():
- dest_path = export_dir / file_path.name
- try:
- shutil.copy2(file_path, dest_path)
- copied_files.append(file_path.name)
- print(f"Copied custom model file: {file_path.name}")
- except Exception as e:
- print(f"Warning: Failed to copy {file_path.name}: {e}")
+ for pattern in custom_file_patterns:
+ for file_path in source_dir.rglob(pattern):
+ if file_path.is_file():
+ rel_path = file_path.relative_to(source_dir)
+ dest_path = export_dir / rel_path
+ dest_path.parent.mkdir(parents=True, exist_ok=True)
+ try:
+ shutil.copy2(file_path, dest_path)
+ copied_files.append(str(rel_path))
+ print(f"Copied custom model file: {rel_path}")
+ except Exception as e:
+ print(f"Warning: Failed to copy {rel_path}: {e}") 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||
if copied_files: | ||||||||||||||||||||||||||||||||||||||||||||||||||
print(f"Successfully copied {len(copied_files)} custom model files to {export_path}") | ||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
print("No custom model files found to copy") |
Uh oh!
There was an error while loading. Please reload this page.