|
| 1 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +"""Unit tests for SeZM MoE All-to-All communication primitive (single-GPU).""" |
| 3 | + |
| 4 | +import unittest |
| 5 | + |
| 6 | +import torch |
| 7 | + |
| 8 | +from deepmd.pt.model.descriptor.sezm_nn.moe.a2a_ops import ( |
| 9 | + all_to_all_differentiable, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class TestAllToAllSingleGPU(unittest.TestCase): |
| 14 | + """Single-GPU tests for _AllToAllDouble communication primitive.""" |
| 15 | + |
| 16 | + def test_single_gpu_passthrough(self): |
| 17 | + """group=None should return x unchanged with gradients flowing through.""" |
| 18 | + x = torch.randn(10, 8, requires_grad=True, device="cpu") |
| 19 | + send_splits = [3, 3, 4] |
| 20 | + recv_splits = [2, 5, 3] |
| 21 | + |
| 22 | + out = all_to_all_differentiable(x, send_splits, recv_splits, group=None) |
| 23 | + |
| 24 | + # Output should be identical to input |
| 25 | + self.assertIs(out, x, "group=None should return input tensor unchanged") |
| 26 | + |
| 27 | + # Gradient should flow through |
| 28 | + loss = out.sum() |
| 29 | + loss.backward() |
| 30 | + self.assertIsNotNone(x.grad, "Gradient should flow through when group=None") |
| 31 | + self.assertTrue( |
| 32 | + torch.allclose(x.grad, torch.ones_like(x)), |
| 33 | + "Gradient should be all ones for sum() loss", |
| 34 | + ) |
| 35 | + |
| 36 | + def test_shape_preservation(self): |
| 37 | + """Forward pass should preserve trailing dimensions.""" |
| 38 | + # Test various shapes |
| 39 | + test_cases = [ |
| 40 | + ((10, 8), [3, 3, 4], [2, 5, 3]), |
| 41 | + ((15, 16, 32), [5, 5, 5], [4, 6, 5]), |
| 42 | + ((8, 4, 4, 64), [2, 3, 3], [3, 2, 3]), |
| 43 | + ] |
| 44 | + |
| 45 | + for shape, send_splits, recv_splits in test_cases: |
| 46 | + with self.subTest(shape=shape): |
| 47 | + x = torch.randn(*shape, device="cpu") |
| 48 | + out = all_to_all_differentiable(x, send_splits, recv_splits, group=None) |
| 49 | + |
| 50 | + # First dimension should match sum(recv_splits) |
| 51 | + expected_shape = (sum(recv_splits), *shape[1:]) |
| 52 | + self.assertEqual( |
| 53 | + out.shape, |
| 54 | + expected_shape, |
| 55 | + f"Output shape mismatch for input shape {shape}", |
| 56 | + ) |
| 57 | + |
| 58 | + def test_first_backward(self): |
| 59 | + """loss.backward() should produce non-zero gradients.""" |
| 60 | + x = torch.randn(10, 8, requires_grad=True, device="cpu") |
| 61 | + send_splits = [3, 3, 4] |
| 62 | + recv_splits = [2, 5, 3] |
| 63 | + |
| 64 | + out = all_to_all_differentiable(x, send_splits, recv_splits, group=None) |
| 65 | + loss = (out**2).sum() |
| 66 | + loss.backward() |
| 67 | + |
| 68 | + self.assertIsNotNone(x.grad, "Gradient should exist after backward") |
| 69 | + self.assertTrue( |
| 70 | + (x.grad.abs() > 1e-6).any(), "Gradient should contain non-zero values" |
| 71 | + ) |
| 72 | + |
| 73 | + def test_second_backward(self): |
| 74 | + """create_graph=True + second backward should produce non-zero gradients.""" |
| 75 | + x = torch.randn(10, 8, requires_grad=True, device="cpu") |
| 76 | + send_splits = [3, 3, 4] |
| 77 | + recv_splits = [2, 5, 3] |
| 78 | + |
| 79 | + # First forward |
| 80 | + out = all_to_all_differentiable(x, send_splits, recv_splits, group=None) |
| 81 | + loss = (out**2).sum() |
| 82 | + |
| 83 | + # First backward with create_graph=True |
| 84 | + (grad_x,) = torch.autograd.grad(loss, x, create_graph=True, retain_graph=True) |
| 85 | + |
| 86 | + self.assertIsNotNone(grad_x, "First-order gradient should exist") |
| 87 | + self.assertTrue( |
| 88 | + grad_x.requires_grad, "First-order gradient should require grad" |
| 89 | + ) |
| 90 | + |
| 91 | + # Second backward |
| 92 | + loss2 = (grad_x**2).sum() |
| 93 | + loss2.backward() |
| 94 | + |
| 95 | + self.assertIsNotNone(x.grad, "Second-order gradient should exist") |
| 96 | + self.assertTrue( |
| 97 | + (x.grad.abs() > 1e-6).any(), |
| 98 | + "Second-order gradient should contain non-zero values", |
| 99 | + ) |
| 100 | + |
| 101 | + def test_gradgradcheck_fp64(self): |
| 102 | + """torch.autograd.gradgradcheck should pass in fp64.""" |
| 103 | + # Use smaller tensors for gradgradcheck (it's expensive) |
| 104 | + x = torch.randn(6, 4, dtype=torch.float64, requires_grad=True, device="cpu") |
| 105 | + send_splits = [2, 2, 2] |
| 106 | + recv_splits = [1, 3, 2] |
| 107 | + |
| 108 | + def func(inp): |
| 109 | + return all_to_all_differentiable(inp, send_splits, recv_splits, group=None) |
| 110 | + |
| 111 | + # gradgradcheck verifies second-order derivatives |
| 112 | + result = torch.autograd.gradgradcheck( |
| 113 | + func, x, eps=1e-6, atol=1e-4, rtol=1e-3, raise_exception=False |
| 114 | + ) |
| 115 | + |
| 116 | + self.assertTrue( |
| 117 | + result, "gradgradcheck failed: second-order derivatives are incorrect" |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + unittest.main() |
0 commit comments