#!/usr/bin/env python3
"""
Test script to verify HuggingFace dynamic module loading with local copies
"""
import sys
import os

print("=" * 80)
print("Testing HuggingFace dynamic module loading")
print("=" * 80)

# Set up paths
model_dir = "/home/chengyuew/chengyuew/workspace/shiyi/LMFlow/src/models/Qwen2_5VL_DFM"

print(f"\n1. Model directory: {model_dir}")
print(f"   Files in directory:")
for f in sorted(os.listdir(model_dir)):
    if f.endswith('.py') or f.endswith('.json'):
        print(f"   - {f}")

print("\n2. Attempting to load config...")
try:
    from transformers import AutoConfig
    config = AutoConfig.from_pretrained(
        model_dir,
        trust_remote_code=True
    )
    print(f"   ✓ Config loaded successfully!")
    print(f"   Model type: {config.model_type}")
except Exception as e:
    print(f"   ✗ Failed to load config:")
    print(f"   Error: {type(e).__name__}: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

print("\n3. Attempting to instantiate model (random weights, CPU)...")
try:
    from transformers import AutoModelForCausalLM
    # Use from_config to instantiate with random weights
    model = AutoModelForCausalLM.from_config(
        config,
        trust_remote_code=True
    )
    print(f"   ✓ Model instantiated successfully!")
    print(f"   Model class: {type(model).__name__}")
    print(f"   Number of parameters: {sum(p.numel() for p in model.parameters()):,}")
except Exception as e:
    print(f"   ✗ Failed to instantiate model:")
    print(f"   Error: {type(e).__name__}: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

print("\n" + "=" * 80)
print("All imports successful!")
print("=" * 80)

