Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions examples/llm_ptq/example_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The 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

  • glob() only matches top-level; subpackages like utils/*.py won’t be copied.
  • Flattening to export_dir/file.name breaks relative imports if a needed file lives in a subdir.

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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}")
copied_files = []
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}")
🤖 Prompt for AI Agents
In examples/llm_ptq/example_utils.py around lines 388 to 399, the current loop
uses glob() and flattens files into export_dir which misses nested files and
breaks relative imports; change to recurse (use rglob or glob with recursive)
over custom_file_patterns, compute each file's relative path to source_dir,
create the corresponding destination subdirectory under export_dir
(mkdir(parents=True, exist_ok=True)), copy the file to that destination path,
and append the relative path (not just name) to copied_files; also keep the
existing try/except and prints but operate on the preserved relative path.

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")
15 changes: 14 additions & 1 deletion examples/llm_ptq/hf_ptq.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
import numpy as np
import torch
from accelerate.hooks import remove_hook_from_module
from example_utils import apply_kv_cache_quant, get_model, get_processor, get_tokenizer, is_enc_dec
from example_utils import (
apply_kv_cache_quant,
copy_custom_model_files,
get_model,
get_processor,
get_tokenizer,
is_enc_dec,
)
from transformers import (
AutoConfig,
AutoModelForCausalLM,
Expand Down Expand Up @@ -604,6 +611,9 @@ def output_decode(generated_ids, input_shape):
inference_tensor_parallel=args.inference_tensor_parallel,
inference_pipeline_parallel=args.inference_pipeline_parallel,
)

# Copy custom model files for TensorRT-LLM export as well
copy_custom_model_files(args.pyt_ckpt_path, export_path, args.trust_remote_code)
else:
# Check arguments for unified_hf export format and set to default if unsupported arguments are provided
assert args.sparsity_fmt == "dense", (
Expand All @@ -621,6 +631,9 @@ def output_decode(generated_ids, input_shape):
export_dir=export_path,
)

# Copy custom model files (configuration_*.py, modeling_*.py, etc.) if trust_remote_code is used
copy_custom_model_files(args.pyt_ckpt_path, export_path, args.trust_remote_code)

# Restore default padding and export the tokenizer as well.
if tokenizer is not None:
tokenizer.padding_side = default_padding_side
Expand Down
Loading