File size: 13,020 Bytes
7667a87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import torch
import torch.nn as nn
from typing import Any, Dict, List, Tuple

def inspect_model_devices(model, prefix="", max_depth=3, current_depth=0):
    """
    Recursively inspect all properties of a PyTorch Lightning model to find
    which tensors are on CPU vs CUDA devices and check density/sparsity.
    
    Args:
        model: The model/object to inspect
        prefix: String prefix for nested attributes
        max_depth: Maximum recursion depth to prevent infinite loops
        current_depth: Current recursion depth
    
    Returns:
        Dict with categorized results
    """
    results = {
        'cuda_tensors': [],
        'cpu_tensors': [],
        'mixed_tensors': [],  # For modules with tensors on different devices
        'sparse_tensors': [],  # Sparse tensors
        'non_contiguous_tensors': [],  # Non-contiguous (non-dense) tensors
        'problematic_tensors': [],  # CPU or sparse or non-contiguous
        'non_tensor_attrs': [],
        'errors': []
    }
    
    if current_depth >= max_depth:
        return results
    
    # Get all attributes of the object
    for attr_name in dir(model):
        # Skip private attributes and methods
        if attr_name.startswith('_'):
            continue
            
        try:
            attr_value = getattr(model, attr_name)
            full_name = f"{prefix}.{attr_name}" if prefix else attr_name
            
            # Check if it's a tensor
            if isinstance(attr_value, torch.Tensor):
                # Check density and sparsity
                is_sparse = attr_value.is_sparse or attr_value.is_sparse_csr
                is_contiguous = attr_value.is_contiguous()
                is_cuda = attr_value.device.type == 'cuda'
                
                device_info = {
                    'name': full_name,
                    'shape': tuple(attr_value.shape),
                    'dtype': str(attr_value.dtype),
                    'device': str(attr_value.device),
                    'requires_grad': attr_value.requires_grad,
                    'is_sparse': is_sparse,
                    'is_contiguous': is_contiguous,
                    'stride': tuple(attr_value.stride()) if not is_sparse else 'N/A (sparse)',
                    'storage_offset': attr_value.storage_offset() if not is_sparse else 'N/A (sparse)',
                    'numel': attr_value.numel()
                }
                
                # Categorize by issues
                has_issues = []
                if not is_cuda:
                    has_issues.append('CPU')
                if is_sparse:
                    has_issues.append('SPARSE')
                    results['sparse_tensors'].append(device_info)
                if not is_contiguous:
                    has_issues.append('NON_CONTIGUOUS')
                    results['non_contiguous_tensors'].append(device_info)
                
                if has_issues:
                    device_info['issues'] = has_issues
                    results['problematic_tensors'].append(device_info)
                
                # Still categorize by device for backward compatibility
                if is_cuda:
                    results['cuda_tensors'].append(device_info)
                else:
                    results['cpu_tensors'].append(device_info)
            
            # Check if it's a Parameter
            elif isinstance(attr_value, nn.Parameter):
                is_sparse = attr_value.is_sparse or attr_value.is_sparse_csr
                is_contiguous = attr_value.is_contiguous()
                is_cuda = attr_value.device.type == 'cuda'
                
                device_info = {
                    'name': full_name,
                    'shape': tuple(attr_value.shape),
                    'dtype': str(attr_value.dtype),
                    'device': str(attr_value.device),
                    'requires_grad': attr_value.requires_grad,
                    'type': 'Parameter',
                    'is_sparse': is_sparse,
                    'is_contiguous': is_contiguous,
                    'stride': tuple(attr_value.stride()) if not is_sparse else 'N/A (sparse)',
                    'storage_offset': attr_value.storage_offset() if not is_sparse else 'N/A (sparse)',
                    'numel': attr_value.numel()
                }
                
                # Categorize by issues
                has_issues = []
                if not is_cuda:
                    has_issues.append('CPU')
                if is_sparse:
                    has_issues.append('SPARSE')
                    results['sparse_tensors'].append(device_info)
                if not is_contiguous:
                    has_issues.append('NON_CONTIGUOUS')
                    results['non_contiguous_tensors'].append(device_info)
                
                if has_issues:
                    device_info['issues'] = has_issues
                    results['problematic_tensors'].append(device_info)
                
                if is_cuda:
                    results['cuda_tensors'].append(device_info)
                else:
                    results['cpu_tensors'].append(device_info)
                    
            # Check if it's a Module (like torchmetrics)
            elif isinstance(attr_value, nn.Module):
                # Get device info for the module
                module_devices = set()
                for param in attr_value.parameters():
                    module_devices.add(param.device.type)
                for buffer in attr_value.buffers():
                    module_devices.add(buffer.device.type)
                
                if len(module_devices) > 1:
                    results['mixed_tensors'].append({
                        'name': full_name,
                        'type': type(attr_value).__name__,
                        'devices': list(module_devices)
                    })
                elif len(module_devices) == 1:
                    device_type = list(module_devices)[0]
                    module_info = {
                        'name': full_name,
                        'type': type(attr_value).__name__,
                        'device': device_type
                    }
                    
                    if device_type == 'cuda':
                        results['cuda_tensors'].append(module_info)
                    else:
                        results['cpu_tensors'].append(module_info)
                
                # Recursively inspect the module
                if current_depth < max_depth - 1:
                    sub_results = inspect_model_devices(attr_value, full_name, max_depth, current_depth + 1)
                    for key in results:
                        results[key].extend(sub_results[key])
            
            # Check for other types that might contain tensors
            elif hasattr(attr_value, '__dict__') and not callable(attr_value):
                if current_depth < max_depth - 1:
                    sub_results = inspect_model_devices(attr_value, full_name, max_depth, current_depth + 1)
                    for key in results:
                        results[key].extend(sub_results[key])
            else:
                # Non-tensor attribute
                if not callable(attr_value):
                    results['non_tensor_attrs'].append({
                        'name': full_name,
                        'type': type(attr_value).__name__,
                        'value': str(attr_value)[:100]  # Truncate long values
                    })
                    
        except Exception as e:
            results['errors'].append({
                'name': full_name,
                'error': str(e)
            })
            continue
    
    return results

def print_device_report(model, detailed=False):
    """
    Print a formatted report of device allocation for all model components.
    
    Args:
        model: PyTorch Lightning model to inspect
        detailed: If True, show detailed information for each tensor
    """
    print("="*80)
    print("COMPREHENSIVE DEVICE & TENSOR DENSITY REPORT")
    print("="*80)
    
    results = inspect_model_devices(model)
    
    # Show problematic tensors first (most important)
    if results['problematic_tensors']:
        print(f"\n🚨 PROBLEMATIC TENSORS ({len(results['problematic_tensors'])}) - LIKELY CAUSING ISSUES!")
        print("-" * 70)
        for item in results['problematic_tensors']:
            issues_str = " | ".join(item['issues'])
            print(f"  ❌ {item['name']}: {issues_str}")
            if detailed:
                print(f"     Shape: {item['shape']} | Device: {item['device']}")
                print(f"     Contiguous: {item['is_contiguous']} | Sparse: {item['is_sparse']}")
                if item['stride'] != 'N/A (sparse)':
                    print(f"     Stride: {item['stride']} | Storage offset: {item['storage_offset']}")
                print()
    
    print(f"\n📍 CUDA TENSORS/MODULES ({len(results['cuda_tensors'])})")
    print("-" * 40)
    for item in results['cuda_tensors']:
        status_indicators = []
        if not item.get('is_contiguous', True):
            status_indicators.append('NON-CONTIGUOUS')
        if item.get('is_sparse', False):
            status_indicators.append('SPARSE')
        
        status_str = f" [{', '.join(status_indicators)}]" if status_indicators else ""
        
        if detailed:
            if 'shape' in item:  # It's a tensor
                print(f"  {item['name']}: {item['shape']} | {item['dtype']} | {item['device']}{status_str}")
                if 'stride' in item and item['stride'] != 'N/A (sparse)':
                    print(f"    ↳ Contiguous: {item.get('is_contiguous', 'N/A')} | Stride: {item['stride']}")
            else:  # It's a module
                print(f"  {item['name']}: {item['type']} | {item['device']}{status_str}")
        else:
            print(f"  ✅ {item['name']}{status_str}")
    
    print(f"\n💻 CPU TENSORS/MODULES ({len(results['cpu_tensors'])})")
    print("-" * 40)
    for item in results['cpu_tensors']:
        status_indicators = []
        if not item.get('is_contiguous', True):
            status_indicators.append('NON-CONTIGUOUS')
        if item.get('is_sparse', False):
            status_indicators.append('SPARSE')
        
        status_str = f" [{', '.join(status_indicators)}]" if status_indicators else ""
        
        if detailed:
            if 'shape' in item:  # It's a tensor
                print(f"  {item['name']}: {item['shape']} | {item['dtype']} | {item['device']}{status_str}")
                if 'stride' in item and item['stride'] != 'N/A (sparse)':
                    print(f"    ↳ Contiguous: {item.get('is_contiguous', 'N/A')} | Stride: {item['stride']}")
            else:  # It's a module
                print(f"  {item['name']}: {item['type']} | {item['device']}{status_str}")
        else:
            print(f"  ❌ {item['name']}{status_str}")
    
    if results['sparse_tensors']:
        print(f"\n🕳️  SPARSE TENSORS ({len(results['sparse_tensors'])})")
        print("-" * 40)
        for item in results['sparse_tensors']:
            print(f"  {item['name']}: {item['shape']} | {item['device']} | SPARSE")
    
    if results['non_contiguous_tensors']:
        print(f"\n📐 NON-CONTIGUOUS TENSORS ({len(results['non_contiguous_tensors'])})")
        print("-" * 40)
        for item in results['non_contiguous_tensors']:
            print(f"  {item['name']}: {item['shape']} | Stride: {item['stride']}")
            if detailed:
                print(f"    ↳ Storage offset: {item['storage_offset']}")
    
    if results['mixed_tensors']:
        print(f"\n⚠️  MIXED DEVICE MODULES ({len(results['mixed_tensors'])})")
        print("-" * 40)
        for item in results['mixed_tensors']:
            print(f"  {item['name']}: {item['type']} | Devices: {item['devices']}")
    
    if results['errors']:
        print(f"\n❗ ERRORS ({len(results['errors'])})")
        print("-" * 40)
        for item in results['errors']:
            print(f"  {item['name']}: {item['error']}")
    
    print(f"\n📊 DETAILED SUMMARY")
    print("-" * 40)
    print(f"  CUDA components: {len(results['cuda_tensors'])}")
    print(f"  CPU components:  {len(results['cpu_tensors'])}")
    print(f"  Sparse tensors:  {len(results['sparse_tensors'])}")
    print(f"  Non-contiguous:  {len(results['non_contiguous_tensors'])}")
    print(f"  Mixed components: {len(results['mixed_tensors'])}")
    print(f"  Errors: {len(results['errors'])}")
    print(f"  🚨 TOTAL PROBLEMATIC: {len(results['problematic_tensors'])}")
    
    if results['problematic_tensors']:
        print(f"\n⚠️  CRITICAL: Found {len(results['problematic_tensors'])} problematic tensors!")
        print("   These are likely causing the 'Tensors must be CUDA and dense' error.")
        print("   Focus on fixing the tensors marked with 🚨 above.")