|
| 1 | +import unittest |
| 2 | +import torch |
| 3 | +from onnx_diagnostic.ext_test_case import ExtTestCase, requires_torch |
| 4 | +from onnx_diagnostic.helpers.torch_test_helper import ( |
| 5 | + is_torchdynamo_exporting, |
| 6 | + fake_torchdynamo_exporting, |
| 7 | +) |
| 8 | +from onnx_diagnostic.helpers import string_type |
| 9 | +from onnx_diagnostic.torch_export_patches.patch_expressions import ( |
| 10 | + _iterate_patched_expressions, |
| 11 | + register_patched_expressions, |
| 12 | + patched_float_arange, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class TestOnnxExportErrors(ExtTestCase): |
| 17 | + |
| 18 | + def test_patched_expressions(self): |
| 19 | + res = list(_iterate_patched_expressions()) |
| 20 | + names = {_[0] for _ in res} |
| 21 | + self.assertIn("float_arange", names) |
| 22 | + |
| 23 | + @requires_torch("2.8") |
| 24 | + def test_filter_position_ids(self): |
| 25 | + |
| 26 | + def filter_position_ids( |
| 27 | + patch_attention_mask: torch.Tensor, |
| 28 | + position_ids: torch.Tensor, |
| 29 | + boundaries: torch.Tensor, |
| 30 | + num_patches_per_side: int, |
| 31 | + ): |
| 32 | + for batch_idx, p_attn_mask in enumerate(patch_attention_mask): |
| 33 | + fractional_coords_h = torch.arange(0, 1 - 1e-6, 1 / p_attn_mask[:, 0].sum()) |
| 34 | + fractional_coords_w = torch.arange(0, 1 - 1e-6, 1 / p_attn_mask[0].sum()) |
| 35 | + |
| 36 | + bucket_coords_h = torch.bucketize(fractional_coords_h, boundaries, right=True) |
| 37 | + bucket_coords_w = torch.bucketize(fractional_coords_w, boundaries, right=True) |
| 38 | + |
| 39 | + pos_ids = ( |
| 40 | + bucket_coords_h[:, None] * num_patches_per_side + bucket_coords_w |
| 41 | + ).flatten() |
| 42 | + position_ids[batch_idx][p_attn_mask.view(-1)] = pos_ids |
| 43 | + return position_ids |
| 44 | + |
| 45 | + def float_arange(start, end, step): |
| 46 | + length = torch.sym_int((end - start) / step + (step * (1 - 1e-6))) |
| 47 | + torch._check(length > 0) |
| 48 | + res = torch.arange(0, length) |
| 49 | + torch._check(res.is_contiguous()) |
| 50 | + fres = res.to(torch.float32) |
| 51 | + fstart = torch.tensor(start, dtype=torch.float32) |
| 52 | + return fres + fstart |
| 53 | + |
| 54 | + def scan_filter_position_ids( |
| 55 | + patch_attention_mask: torch.Tensor, |
| 56 | + position_ids: torch.Tensor, |
| 57 | + boundaries: torch.Tensor, |
| 58 | + num_patches_per_side: int, |
| 59 | + ): |
| 60 | + |
| 61 | + def body(p_attn_mask, position_ids_row): |
| 62 | + h_len = torch.tensor(1) / p_attn_mask[:, 0].sum() |
| 63 | + w_len = torch.tensor(1) / p_attn_mask[0].sum() |
| 64 | + fractional_coords_h = patched_float_arange( |
| 65 | + torch.tensor(0.0), torch.tensor(1 - 1e-6), h_len |
| 66 | + ) |
| 67 | + fractional_coords_w = patched_float_arange( |
| 68 | + torch.tensor(0.0), torch.tensor(1 - 1e-6), w_len |
| 69 | + ) |
| 70 | + |
| 71 | + # torch.arange(0, 1 - 1e-6, 1 / p_attn_mask[:, 0].sum().item()) |
| 72 | + # torch.arange(0, 1 - 1e-6, 1 / p_attn_mask[0].sum().item()) |
| 73 | + |
| 74 | + bucket_coords_h = torch.bucketize(fractional_coords_h, boundaries, right=True) |
| 75 | + bucket_coords_w = torch.bucketize(fractional_coords_w, boundaries, right=True) |
| 76 | + |
| 77 | + pos_ids = ( |
| 78 | + bucket_coords_h[:, None] * num_patches_per_side + bucket_coords_w |
| 79 | + ).flatten() |
| 80 | + |
| 81 | + row = position_ids_row.clone() |
| 82 | + row[p_attn_mask.view(-1)] = pos_ids |
| 83 | + return [row] |
| 84 | + |
| 85 | + return torch.ops.higher_order.scan( |
| 86 | + body, [], [patch_attention_mask, position_ids], additional_inputs=[] |
| 87 | + ) |
| 88 | + |
| 89 | + class Model(torch.nn.Module): |
| 90 | + def forward(self, patch_attention_mask, position_ids, boundaries): |
| 91 | + if is_torchdynamo_exporting(): |
| 92 | + res = scan_filter_position_ids( |
| 93 | + patch_attention_mask, position_ids, boundaries, 32 |
| 94 | + ) |
| 95 | + return res[0] |
| 96 | + return filter_position_ids(patch_attention_mask, position_ids, boundaries, 32) |
| 97 | + |
| 98 | + # 32 |
| 99 | + # T9s32x32x32[False,True:A0.978515625], |
| 100 | + # T7s32x1024[0,0:A0.0], |
| 101 | + # T1s31[0.03125,0.96875:A0.5]] |
| 102 | + register_patched_expressions() |
| 103 | + patch_attention_mask = torch.randint(0, 20, (32, 32, 32)) >= 1 |
| 104 | + patch_attention_mask[:, :, :] = True |
| 105 | + position_ids = torch.zeros((32, 1024), dtype=torch.int64) |
| 106 | + boundaries = (torch.arange(33).to(torch.float32) / 33)[1:-1] |
| 107 | + inputs = (patch_attention_mask, position_ids, boundaries) |
| 108 | + model = Model() |
| 109 | + expected = model(*inputs) |
| 110 | + with fake_torchdynamo_exporting(): |
| 111 | + got = model(*inputs) |
| 112 | + self.assertEqual(type(expected), type(got)) |
| 113 | + self.assertEqual( |
| 114 | + string_type(expected, with_shape=True), string_type(got, with_shape=True) |
| 115 | + ) |
| 116 | + self.assertEqualArray(expected, got) |
| 117 | + |
| 118 | + DYN = torch.export.Dim.DYNAMIC |
| 119 | + ep = torch.export.export(model, inputs, dynamic_shapes=({0: DYN}, {0: DYN}, {0: DYN})) |
| 120 | + self.assertEqualArray(expected, ep.module()(*inputs)) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + unittest.main(verbosity=2) |
0 commit comments