|
6 | 6 |
|
7 | 7 | # pyre-strict |
8 | 8 | import copy |
| 9 | +import itertools |
9 | 10 | import os |
10 | 11 | import tempfile |
11 | 12 | import unittest |
|
41 | 42 | from executorch.exir.graph_module import get_control_flow_submodules |
42 | 43 | from executorch.exir.pass_base import ExportPass, PassResult |
43 | 44 | from executorch.exir.passes import ( |
| 45 | + convert_constant_dim_order_pass, |
44 | 46 | dead_code_elimination_pass, |
45 | 47 | DebugPass, |
46 | 48 | HintBasedSymShapeEvalPass, |
@@ -2330,3 +2332,155 @@ def forward(self, x): |
2330 | 2332 | prop_tensor.is_contiguous(), |
2331 | 2333 | f"Propagated tensor is not contiguous: {prop_tensor.stride()}", |
2332 | 2334 | ) |
| 2335 | + |
| 2336 | + class AddConstant(torch.nn.Module): |
| 2337 | + def __init__(self, constant: torch.Tensor, use_buffer: bool = False): |
| 2338 | + super().__init__() |
| 2339 | + if use_buffer: |
| 2340 | + self.register_buffer("constant", constant) |
| 2341 | + else: |
| 2342 | + self.constant = constant |
| 2343 | + |
| 2344 | + def forward(self, x): |
| 2345 | + return x + self.constant |
| 2346 | + |
| 2347 | + def test_convert_constant_dim_order_noop(self): |
| 2348 | + """ |
| 2349 | + Verify that the convert_constant_dim_order_pass does not modify |
| 2350 | + constant tensors in contiguous or channels last format. |
| 2351 | + """ |
| 2352 | + |
| 2353 | + memory_formats = [ |
| 2354 | + torch.contiguous_format, |
| 2355 | + torch.channels_last, |
| 2356 | + ] |
| 2357 | + |
| 2358 | + use_buffer_options = [False, True] |
| 2359 | + |
| 2360 | + for memory_format, use_buffer in itertools.product( |
| 2361 | + memory_formats, use_buffer_options |
| 2362 | + ): |
| 2363 | + constant = torch.randn(2, 3, 4, 5).to(memory_format=memory_format) |
| 2364 | + model = self.AddConstant(constant, use_buffer=use_buffer) |
| 2365 | + inputs = (torch.randn(2, 3, 4, 5).to(memory_format=memory_format),) |
| 2366 | + |
| 2367 | + ep = torch.export.export(model, inputs) |
| 2368 | + modified_ep = ( |
| 2369 | + convert_constant_dim_order_pass.convert_constant_dim_order_pass( |
| 2370 | + copy.deepcopy(ep) |
| 2371 | + ) |
| 2372 | + ) |
| 2373 | + |
| 2374 | + # Check the outputs - they should match in both data and dim order. |
| 2375 | + original_outputs = ep.module()(*inputs) |
| 2376 | + modified_outputs = modified_ep.module()(*inputs) |
| 2377 | + |
| 2378 | + torch.testing.assert_close( |
| 2379 | + original_outputs, modified_outputs, check_stride=True |
| 2380 | + ) |
| 2381 | + |
| 2382 | + # Verify that the constant/buffer tensor is not modified. The interface for |
| 2383 | + # constants and buffers is just different enough to make it hard to unify the |
| 2384 | + # below logic. |
| 2385 | + if use_buffer: |
| 2386 | + original_buffers = dict(ep.named_buffers()) |
| 2387 | + modified_buffers = dict(modified_ep.named_buffers()) |
| 2388 | + |
| 2389 | + self.assertEqual(len(original_buffers), 1) |
| 2390 | + self.assertEqual(len(modified_buffers), 1) |
| 2391 | + |
| 2392 | + buffer_key = next(iter(original_buffers.keys())) |
| 2393 | + original_buffer = original_buffers[buffer_key] |
| 2394 | + modified_buffer = modified_buffers[buffer_key] |
| 2395 | + |
| 2396 | + torch.testing.assert_close( |
| 2397 | + original_buffer, modified_buffer, check_stride=True |
| 2398 | + ) |
| 2399 | + else: |
| 2400 | + self.assertEqual(len(ep.constants), 1) |
| 2401 | + self.assertEqual(len(modified_ep.constants), 1) |
| 2402 | + |
| 2403 | + const_key = next(iter(ep.constants.keys())) |
| 2404 | + original_const = ep.constants[const_key] |
| 2405 | + modified_const = modified_ep.constants[const_key] |
| 2406 | + |
| 2407 | + torch.testing.assert_close( |
| 2408 | + original_const, modified_const, check_stride=True |
| 2409 | + ) |
| 2410 | + |
| 2411 | + def test_convert_constant_dim_order_to_contiguous(self): |
| 2412 | + """ |
| 2413 | + Verify that the convert_constant_dim_order_pass converts constant/buffer |
| 2414 | + tensors with dim orders / memory formats outside the allowed list |
| 2415 | + (contiguous and channels_last) to contiguous. |
| 2416 | + """ |
| 2417 | + |
| 2418 | + # Test cases using transpose/permute to create non-contiguous tensors. |
| 2419 | + # Each case is (base_shape, permutation, description) where we create a |
| 2420 | + # contiguous tensor of base_shape and then permute/transpose it. |
| 2421 | + test_cases = [ |
| 2422 | + # Rank 2 - transposed matrix (non-contiguous strides) |
| 2423 | + ((5, 4), (1, 0), "2D transposed"), |
| 2424 | + # Rank 3 - permuted dimensions |
| 2425 | + ((4, 3, 2), (2, 0, 1), "3D permuted"), |
| 2426 | + # Rank 3 - another permutation |
| 2427 | + ((2, 4, 3), (1, 2, 0), "3D permuted v2"), |
| 2428 | + # Rank 4 - permuted (not contiguous, not channels_last) |
| 2429 | + ((5, 4, 3, 2), (3, 1, 0, 2), "4D permuted"), |
| 2430 | + # Rank 5 - permuted |
| 2431 | + ((6, 5, 4, 3, 2), (4, 2, 0, 3, 1), "5D permuted"), |
| 2432 | + ] |
| 2433 | + |
| 2434 | + use_buffer_options = [False, True] |
| 2435 | + |
| 2436 | + for (base_shape, permutation, description), use_buffer in itertools.product( |
| 2437 | + test_cases, use_buffer_options |
| 2438 | + ): |
| 2439 | + with self.subTest(description=description, use_buffer=use_buffer): |
| 2440 | + # Create a contiguous tensor and permute it to make it non-contiguous |
| 2441 | + base_tensor = torch.randn(base_shape) |
| 2442 | + constant = base_tensor.permute(permutation) |
| 2443 | + |
| 2444 | + # Sanity check the tensor construction |
| 2445 | + self.assertFalse( |
| 2446 | + constant.is_contiguous(), |
| 2447 | + "Test setup error: tensor should be non-contiguous", |
| 2448 | + ) |
| 2449 | + |
| 2450 | + model = self.AddConstant(constant, use_buffer=use_buffer) |
| 2451 | + inputs = (torch.randn(constant.shape),) |
| 2452 | + |
| 2453 | + ep = torch.export.export(model, inputs) |
| 2454 | + modified_ep = ( |
| 2455 | + convert_constant_dim_order_pass.convert_constant_dim_order_pass( |
| 2456 | + copy.deepcopy(ep) |
| 2457 | + ) |
| 2458 | + ) |
| 2459 | + |
| 2460 | + # Check the outputs - they should match in data. |
| 2461 | + original_outputs = ep.module()(*inputs) |
| 2462 | + modified_outputs = modified_ep.module()(*inputs) |
| 2463 | + torch.testing.assert_close(original_outputs, modified_outputs) |
| 2464 | + |
| 2465 | + # Verify that the constant/buffer tensor is now contiguous. |
| 2466 | + if use_buffer: |
| 2467 | + modified_buffers = dict(modified_ep.named_buffers()) |
| 2468 | + self.assertEqual(len(modified_buffers), 1) |
| 2469 | + |
| 2470 | + buffer_key = next(iter(modified_buffers.keys())) |
| 2471 | + modified_buffer = modified_buffers[buffer_key] |
| 2472 | + |
| 2473 | + self.assertTrue( |
| 2474 | + modified_buffer.is_contiguous(), |
| 2475 | + f"Buffer should be contiguous after pass, got strides {modified_buffer.stride()}", |
| 2476 | + ) |
| 2477 | + else: |
| 2478 | + self.assertEqual(len(modified_ep.constants), 1) |
| 2479 | + |
| 2480 | + const_key = next(iter(modified_ep.constants.keys())) |
| 2481 | + modified_const = modified_ep.constants[const_key] |
| 2482 | + |
| 2483 | + self.assertTrue( |
| 2484 | + modified_const.is_contiguous(), |
| 2485 | + f"Constant should be contiguous after pass, got strides {modified_const.stride()}", |
| 2486 | + ) |
0 commit comments