|
| 1 | +import os |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch_xla |
| 5 | +import torch_xla.core.xla_model as xm |
| 6 | +import torch_xla.utils.utils as xu |
| 7 | +import torch_xla.debug.profiler as xp |
| 8 | +import torch_xla.utils.utils as xu |
| 9 | +import torch_xla.distributed.parallel_loader as pl |
| 10 | +import unittest |
| 11 | + |
| 12 | + |
| 13 | +def check_env_flag(name, default=''): |
| 14 | + return os.getenv(name, default).upper() in ['ON', '1', 'YES', 'TRUE', 'Y'] |
| 15 | + |
| 16 | + |
| 17 | +def extract_execution_cause(lines): |
| 18 | + causes = [] |
| 19 | + for i in range(len(lines)): |
| 20 | + if 'Execution Cause' in lines[i].decode(): |
| 21 | + causes.append(lines[i + 1].decode()) |
| 22 | + return causes |
| 23 | + |
| 24 | + |
| 25 | +class PtXLADebugTest(unittest.TestCase): |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def setUpClass(cls): |
| 29 | + if not check_env_flag('PT_XLA_DEBUG'): |
| 30 | + assert False, "This test should be run with PT_XLA_DEBUG" |
| 31 | + cls.debug_file_name = os.getenv('PT_XLA_DEBUG_FILE') |
| 32 | + if not cls.debug_file_name: |
| 33 | + assert False, "This test should be run with PT_XLA_DEBUG_FILE" |
| 34 | + open(cls.debug_file_name, 'w').close() |
| 35 | + |
| 36 | + def test_user_mark_step(self): |
| 37 | + device = xm.xla_device() |
| 38 | + t1 = torch.randn(2, 2, device=device) |
| 39 | + xm.mark_step() |
| 40 | + with open(self.debug_file_name, 'rb') as f: |
| 41 | + lines = f.readlines() |
| 42 | + causes = extract_execution_cause(lines) |
| 43 | + self.assertEqual(len(causes), 1) |
| 44 | + self.assertIn('user mark_step', causes[0]) |
| 45 | + open(self.debug_file_name, 'w').close() |
| 46 | + |
| 47 | + def test_step_trace(self): |
| 48 | + device = xm.xla_device() |
| 49 | + with xp.StepTrace('train_pt_xla_debug'): |
| 50 | + t1 = torch.randn(2, 2, device=device) |
| 51 | + with open(self.debug_file_name, 'rb') as f: |
| 52 | + lines = f.readlines() |
| 53 | + causes = extract_execution_cause(lines) |
| 54 | + self.assertEqual(len(causes), 1) |
| 55 | + self.assertIn('mark_step when exiting a profiler StepTrace region', |
| 56 | + causes[0]) |
| 57 | + open(self.debug_file_name, 'w').close() |
| 58 | + |
| 59 | + def test_dynamo(self): |
| 60 | + device = xm.xla_device() |
| 61 | + t1 = torch.randn(2, 2, device=device) |
| 62 | + |
| 63 | + def toy_program(t1): |
| 64 | + return t1 + t1 |
| 65 | + |
| 66 | + compiled = torch.compile(toy_program, backend="openxla") |
| 67 | + res = compiled(t1) |
| 68 | + with open(self.debug_file_name, 'rb') as f: |
| 69 | + lines = f.readlines() |
| 70 | + causes = extract_execution_cause(lines) |
| 71 | + self.assertEqual(len(causes), 3) |
| 72 | + self.assertIn('mark_step when dynamo processing input graphs', causes[0]) |
| 73 | + self.assertIn('mark_step when dynamo processing input graphs', causes[1]) |
| 74 | + self.assertIn('dynamo compiles FX graph to HLO', causes[2]) |
| 75 | + open(self.debug_file_name, 'w').close() |
| 76 | + |
| 77 | + def test_parallel_loader(self): |
| 78 | + device = xm.xla_device() |
| 79 | + |
| 80 | + train_dataset_len = 100 |
| 81 | + batch_size = 10 |
| 82 | + train_loader = xu.SampleGenerator( |
| 83 | + data=(torch.zeros(batch_size, 3, 128, |
| 84 | + 128), torch.zeros(batch_size, dtype=torch.int64)), |
| 85 | + sample_count=train_dataset_len // 10) |
| 86 | + |
| 87 | + train_device_loader = pl.MpDeviceLoader( |
| 88 | + train_loader, |
| 89 | + device, |
| 90 | + loader_prefetch_size=8, |
| 91 | + device_prefetch_size=4, |
| 92 | + host_to_device_transfer_threads=1) |
| 93 | + |
| 94 | + for step, (data, target) in enumerate(train_device_loader): |
| 95 | + pass |
| 96 | + |
| 97 | + with open(self.debug_file_name, 'rb') as f: |
| 98 | + lines = f.readlines() |
| 99 | + causes = extract_execution_cause(lines) |
| 100 | + self.assertEqual(len(causes), batch_size + 2) |
| 101 | + for cause in causes: |
| 102 | + self.assertIn('mark_step in parallel loader at step end', cause) |
| 103 | + open(self.debug_file_name, 'w').close() |
| 104 | + |
| 105 | + def test_print(self): |
| 106 | + device = xm.xla_device() |
| 107 | + t1 = torch.randn(2, 2, device=device) |
| 108 | + print(t1) |
| 109 | + with open(self.debug_file_name, 'rb') as f: |
| 110 | + lines = f.readlines() |
| 111 | + causes = extract_execution_cause(lines) |
| 112 | + self.assertEqual(len(causes), 1) |
| 113 | + self.assertIn('user code trying to access tensor value', causes[0]) |
| 114 | + open(self.debug_file_name, 'w').close() |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + test = unittest.main() |
| 119 | + sys.exit(0 if test.result.wasSuccessful() else 1) |
0 commit comments