Skip to content

Commit 96e1613

Browse files
committed
update
Signed-off-by: Zhiyu Cheng <[email protected]>
1 parent f849c17 commit 96e1613

File tree

2 files changed

+34
-59
lines changed

2 files changed

+34
-59
lines changed

examples/llm_ptq/hf_ptq.py

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -660,48 +660,15 @@ def output_decode(generated_ids, input_shape):
660660
export_path
661661
)
662662

663-
# Try to save processor config if available (skip for Nemotron VL models)
664-
if not is_nemotron_vl:
665-
try:
666-
print(f"Saving processor config to {export_path}")
667-
AutoProcessor.from_pretrained(
668-
args.pyt_ckpt_path, trust_remote_code=args.trust_remote_code
669-
).save_pretrained(export_path)
670-
except Exception as e:
671-
print(f"Warning: Could not save processor config: {e}")
672-
print("This is normal for some VLM architectures that don't use AutoProcessor")
673-
else:
674-
print("Skipping AutoProcessor for Nemotron VL (uses separate AutoImageProcessor)")
675-
676-
# For Nemotron VL models, save image processor using proper HuggingFace APIs
677-
if is_nemotron_vl:
678-
import os
679-
import shutil
680-
681-
# Try to save image processor config using HuggingFace API
682-
try:
683-
print("Saving image processor config using AutoImageProcessor...")
684-
image_processor = AutoImageProcessor.from_pretrained(
685-
args.pyt_ckpt_path, trust_remote_code=args.trust_remote_code
686-
)
687-
image_processor.save_pretrained(export_path)
688-
print(" ✅ Image processor config saved successfully")
689-
except Exception as e:
690-
print(f" Warning: Could not save image processor config: {e}")
691-
692-
# Manually copy image_processing.py as it contains custom code that save_pretrained doesn't handle
693-
print("Copying custom image processing implementation...")
694-
src_path = os.path.join(args.pyt_ckpt_path, "image_processing.py")
695-
dst_path = os.path.join(export_path, "image_processing.py")
696-
697-
if os.path.exists(src_path):
698-
try:
699-
shutil.copy2(src_path, dst_path)
700-
print(" ✅ Copied: image_processing.py")
701-
except Exception as copy_e:
702-
print(f" Warning: Could not copy image_processing.py: {copy_e}")
703-
else:
704-
print(" Warning: image_processing.py not found in source model")
663+
# Try to save processor config if available
664+
try:
665+
print(f"Saving processor config to {export_path}")
666+
AutoProcessor.from_pretrained(
667+
args.pyt_ckpt_path, trust_remote_code=args.trust_remote_code
668+
).save_pretrained(export_path)
669+
except Exception as e:
670+
print(f"Warning: Could not save processor config: {e}")
671+
print("This is normal for some VLM architectures that don't use AutoProcessor")
705672

706673
if model_type == "mllama":
707674
full_model_config = model.config

examples/llm_ptq/vlm_utils.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,37 @@ def run_vl_preview_generation(model, tokenizer, model_path, stage_name):
4040
script_dir = os.path.dirname(os.path.abspath(__file__))
4141
images_dir = os.path.join(script_dir, "images")
4242

43+
# Check if images directory exists
44+
if not os.path.exists(images_dir):
45+
print(f"❌ Warning: Images directory not found at {images_dir}")
46+
print(" VL preview generation requires sample images to test vision capabilities.")
47+
print(" Skipping VL preview generation.")
48+
return None
49+
4350
# Use single image for VL preview to avoid shape mismatch issues
44-
image_files = ["example1a.jpeg", "example1b.jpeg"]
51+
image_files = ["example1a.jpeg", "example1b.jpeg", "example.jpg", "test.jpg", "sample.png"]
4552
image = None
53+
missing_files = []
4654
for img_file in image_files:
4755
img_path = os.path.join(images_dir, img_file)
4856
if os.path.exists(img_path):
49-
image = Image.open(img_path)
50-
print(f" Loaded: {img_file}")
51-
break # Use the first available image
57+
try:
58+
image = Image.open(img_path)
59+
print(f" ✅ Successfully loaded: {img_file}")
60+
break # Use the first available image
61+
except Exception as e:
62+
print(f" ⚠️ Warning: Could not open {img_file}: {e}")
63+
missing_files.append(f"{img_file} (corrupted)")
5264
else:
53-
print(f" Warning: {img_file} not found")
65+
missing_files.append(img_file)
5466

5567
if image is None:
56-
print("No sample images found - skipping VL preview generation")
68+
print(f"❌ Warning: No valid sample images found in {images_dir}")
69+
print(f" Searched for: {', '.join(image_files)}")
70+
if missing_files:
71+
print(f" Missing/invalid files: {', '.join(missing_files)}")
72+
print(" VL preview generation requires sample images to test vision capabilities.")
73+
print(" Skipping VL preview generation.")
5774
return None
5875

5976
# Generate response
@@ -66,13 +83,10 @@ def run_vl_preview_generation(model, tokenizer, model_path, stage_name):
6683

6784
print(f"Generating VL response ({stage_name})...")
6885

69-
# Try to detect if this is a v1 model (has chat method) or v2 model (uses generate)
86+
# Try to detect the VL model has chat method or generate method
7087
if hasattr(model, "chat"):
71-
print(" Using v1 model.chat() method...")
72-
# Load image processor for v1 models
7388
image_processor = AutoImageProcessor.from_pretrained(model_path, trust_remote_code=True)
7489

75-
# Process single image for v1 models
7690
image_features = image_processor([image]) # Pass as list with single image
7791

7892
# Move image features to the same device as the model
@@ -89,11 +103,8 @@ def run_vl_preview_generation(model, tokenizer, model_path, stage_name):
89103
**image_features,
90104
)
91105
else:
92-
print(" Using v2 model.generate() method...")
93-
# Load processor for v2 models
94106
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
95107

96-
# Create messages in the format expected by v2 models
97108
messages = [
98109
{"role": "system", "content": "/no_think"},
99110
{
@@ -159,7 +170,7 @@ def run_vl_preview_generation(model, tokenizer, model_path, stage_name):
159170

160171

161172
def run_text_only_generation(model, tokenizer, question, generation_config, model_path):
162-
"""Run text-only generation for VL models, supporting both v1 (chat) and v2 (generate) models.
173+
"""Run text-only generation for VL models, supporting both chat and generate methods.
163174
164175
Args:
165176
model: The VL model
@@ -173,13 +184,10 @@ def run_text_only_generation(model, tokenizer, question, generation_config, mode
173184
"""
174185
try:
175186
if hasattr(model, "chat"):
176-
print(" Using v1 model.chat() method for text-only generation...")
177187
# Use model.chat with None for images (text-only mode)
178188
response = model.chat(tokenizer, None, question, generation_config, history=None)
179189
return response
180190
else:
181-
print(" Using v2 model.generate() method for text-only generation...")
182-
# Load processor for v2 models
183191
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
184192

185193
# Create text-only messages

0 commit comments

Comments
 (0)