mknolan commited on
Commit
2822462
·
verified ·
1 Parent(s): 56d4429

Upload simple_internvit_test.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. simple_internvit_test.py +109 -0
simple_internvit_test.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import os
3
+ import sys
4
+ import traceback
5
+ import gradio as gr
6
+ from PIL import Image
7
+ from transformers import AutoModel, CLIPImageProcessor
8
+
9
+ print("=" * 50)
10
+ print("SIMPLE INTERNVIT-6B MODEL LOADING TEST")
11
+ print("=" * 50)
12
+
13
+ # System information
14
+ print(f"Python version: {sys.version}")
15
+ print(f"PyTorch version: {torch.__version__}")
16
+ print(f"CUDA available: {torch.cuda.is_available()}")
17
+
18
+ if torch.cuda.is_available():
19
+ print(f"CUDA version: {torch.version.cuda}")
20
+ print(f"GPU count: {torch.cuda.device_count()}")
21
+ for i in range(torch.cuda.device_count()):
22
+ print(f"GPU {i}: {torch.cuda.get_device_name(i)}")
23
+
24
+ # Memory info
25
+ print(f"Total GPU memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB")
26
+ print(f"Allocated GPU memory: {torch.cuda.memory_allocated() / 1e9:.2f} GB")
27
+ print(f"Reserved GPU memory: {torch.cuda.memory_reserved() / 1e9:.2f} GB")
28
+ else:
29
+ print("CUDA is not available. This is a critical issue for model loading.")
30
+
31
+ # Create a function to load and test the model
32
+ def load_and_test_model():
33
+ try:
34
+ print("\nLoading model with bfloat16 precision and low_cpu_mem_usage=True...")
35
+ model = AutoModel.from_pretrained(
36
+ 'OpenGVLab/InternViT-6B-224px',
37
+ torch_dtype=torch.bfloat16,
38
+ low_cpu_mem_usage=True,
39
+ trust_remote_code=True)
40
+
41
+ if torch.cuda.is_available():
42
+ print("Moving model to CUDA...")
43
+ model = model.cuda()
44
+
45
+ model.eval()
46
+ print("✓ Model loaded successfully!")
47
+
48
+ # Now try to process a test image
49
+ print("\nLoading image processor...")
50
+ image_processor = CLIPImageProcessor.from_pretrained('OpenGVLab/InternViT-6B-224px')
51
+ print("✓ Image processor loaded successfully!")
52
+
53
+ # Create a simple test image
54
+ print("\nCreating test image...")
55
+ test_image = Image.new('RGB', (224, 224), color='red')
56
+
57
+ # Process the test image
58
+ print("Processing test image...")
59
+ pixel_values = image_processor(images=test_image, return_tensors='pt').pixel_values
60
+ if torch.cuda.is_available():
61
+ pixel_values = pixel_values.to(torch.bfloat16).cuda()
62
+
63
+ # Get model parameters
64
+ params = sum(p.numel() for p in model.parameters())
65
+ print(f"Model parameters: {params:,}")
66
+
67
+ # Forward pass
68
+ print("Running forward pass...")
69
+ with torch.no_grad():
70
+ outputs = model(pixel_values)
71
+
72
+ print("✓ Forward pass successful!")
73
+ print(f"Output shape: {outputs.last_hidden_state.shape}")
74
+
75
+ return f"SUCCESS: Model loaded and test passed!\nParameters: {params:,}\nOutput shape: {outputs.last_hidden_state.shape}"
76
+
77
+ except Exception as e:
78
+ print(f"\n❌ ERROR: {str(e)}")
79
+ traceback.print_exc()
80
+ return f"FAILED: Error loading model or processing image\nError: {str(e)}"
81
+
82
+ # Create a simple Gradio interface
83
+ def create_interface():
84
+ with gr.Blocks(title="InternViT-6B Test") as demo:
85
+ gr.Markdown("# InternViT-6B Model Loading Test")
86
+
87
+ with gr.Row():
88
+ test_btn = gr.Button("Test Model Loading")
89
+ output = gr.Textbox(label="Test Results", lines=10)
90
+
91
+ test_btn.click(fn=load_and_test_model, inputs=[], outputs=output)
92
+
93
+ return demo
94
+
95
+ # Main function
96
+ if __name__ == "__main__":
97
+ # Print environment variables
98
+ print("\nEnvironment variables:")
99
+ relevant_vars = ["CUDA_VISIBLE_DEVICES", "NVIDIA_VISIBLE_DEVICES",
100
+ "TRANSFORMERS_CACHE", "HF_HOME", "PYTORCH_CUDA_ALLOC_CONF"]
101
+ for var in relevant_vars:
102
+ print(f"{var}: {os.environ.get(var, 'Not set')}")
103
+
104
+ # Set environment variable for better GPU memory management
105
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
106
+
107
+ # Create and launch the interface
108
+ demo = create_interface()
109
+ demo.launch(share=False, server_name="0.0.0.0")