-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_torch_28_compatibility.py
More file actions
191 lines (155 loc) · 5.95 KB
/
Copy pathtest_torch_28_compatibility.py
File metadata and controls
191 lines (155 loc) · 5.95 KB
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
#!/usr/bin/env python3
"""
PyTorch 2.8.0 + torch-xla 2.8 compatibility test for Diff-VocSep TPU code.
This script tests that all TPU-related code is compatible with torch-xla 2.8.x.
Run this in a TPU environment with torch-xla 2.8 installed to verify compatibility.
Usage:
python test_torch_28_compatibility.py
"""
import os
import sys
import importlib
import traceback
from typing import List, Tuple
def test_environment_setup() -> bool:
"""Test PJRT environment setup for torch-xla 2.8+."""
print("Testing PJRT environment setup...")
try:
os.environ.setdefault("PJRT_DEVICE", "TPU")
os.environ.setdefault("XLA_USE_SPMD", "1")
print("✓ PJRT environment variables set")
return True
except Exception as e:
print(f"✗ PJRT environment setup failed: {e}")
return False
def test_pytorch_compatibility() -> bool:
"""Test PyTorch 2.8.0 compatibility."""
print("Testing PyTorch 2.8.0 features...")
try:
import torch
version = torch.__version__
print(f"✓ PyTorch version: {version}")
# Test autocast with XLA device_type
with torch.autocast(device_type="xla", dtype=torch.bfloat16, enabled=True):
x = torch.randn(2, 3, 64, 128)
y = torch.clamp(x, -1.0, 1.0)
z = torch.cat([x, y], dim=1)
print("✓ torch.autocast with device_type='xla' works")
# Test distributed functions
import torch.distributed as dist
required_funcs = ['init_process_group', 'get_world_size', 'get_rank']
for func in required_funcs:
if hasattr(dist, func):
print(f"✓ torch.distributed.{func} available")
else:
print(f"✗ torch.distributed.{func} missing")
return False
return True
except Exception as e:
print(f"✗ PyTorch compatibility test failed: {e}")
traceback.print_exc()
return False
def test_torch_xla_lazy_imports() -> bool:
"""Test torch-xla lazy import patterns."""
print("Testing torch-xla lazy import patterns...")
try:
# Test lazy import of torch_xla modules
modules_to_test = [
"torch_xla.core.xla_model",
"torch_xla.distributed.parallel_loader",
"torch_xla.distributed.xla_multiprocessing"
]
for module_name in modules_to_test:
try:
module = importlib.import_module(module_name)
print(f"✓ {module_name} imported successfully")
except ImportError:
print(f"⚠️ {module_name} not available (expected if not in TPU environment)")
return True
except Exception as e:
print(f"✗ torch-xla lazy import test failed: {e}")
return False
def test_torch_xla_functionality() -> bool:
"""Test torch-xla specific functionality if available."""
print("Testing torch-xla functionality...")
try:
xm = importlib.import_module("torch_xla.core.xla_model")
# Test basic xm functions exist
required_functions = [
'xla_device', 'is_master_ordinal', 'master_print',
'mark_step', 'all_reduce', 'get_ordinal', 'xla_device_world_size'
]
for func_name in required_functions:
if hasattr(xm, func_name):
print(f"✓ xm.{func_name} available")
else:
print(f"✗ xm.{func_name} missing")
return False
# Test that deprecated functions are avoided
if hasattr(xm, 'xrt_world_size'):
print("⚠️ xm.xrt_world_size still available but deprecated in 2.8")
return True
except ImportError:
print("⚠️ torch-xla not available (expected if not in TPU environment)")
return True # This is OK for testing
except Exception as e:
print(f"✗ torch-xla functionality test failed: {e}")
return False
def test_tpu_code_imports() -> bool:
"""Test that TPU code files can be imported without errors."""
print("Testing TPU code imports...")
try:
# Add current directory to path
sys.path.insert(0, os.getcwd())
# Import basic modules used by TPU code
import torch
import importlib
import yaml
import math
from torch import optim
from torch.utils.data import DataLoader, DistributedSampler
import torch.distributed as dist
print("✓ All basic imports successful")
return True
except Exception as e:
print(f"✗ TPU code import test failed: {e}")
return False
def run_compatibility_tests() -> Tuple[int, int]:
"""Run all compatibility tests."""
tests = [
test_environment_setup,
test_pytorch_compatibility,
test_torch_xla_lazy_imports,
test_torch_xla_functionality,
test_tpu_code_imports
]
passed = 0
total = len(tests)
for test_func in tests:
print("-" * 50)
try:
if test_func():
passed += 1
print(f"✅ {test_func.__name__} PASSED")
else:
print(f"❌ {test_func.__name__} FAILED")
except Exception as e:
print(f"❌ {test_func.__name__} FAILED with exception: {e}")
return passed, total
def main():
"""Main function."""
print("PyTorch 2.8.0 + torch-xla 2.8 Compatibility Test")
print("=" * 60)
passed, total = run_compatibility_tests()
print("=" * 60)
print(f"Test Results: {passed}/{total} tests passed")
if passed == total:
print("🎉 All compatibility tests passed!")
print("✅ TPU code is ready for torch-xla 2.8")
return 0
else:
print(f"⚠️ {total - passed} tests failed or had warnings")
print("Check the output above for details")
return 1
if __name__ == "__main__":
exit(main())