test_aten.py 3.63 KB
Newer Older
1
2
from typing import Any, Callable, Union

3
import pytest
4
5
6
import torch
import torch.nn as nn

7
8
from colossalai.testing import clear_cache_before_run

9
10
11
12
13
14
15
16
try:
    from colossalai._analyzer._subclasses import MetaTensor
except:
    pass

aten = torch.ops.aten

registered_meta = {
17
    ("aten.convolution.default", True): [  # (aten ops, requires_backward)
18
19
20
21
        (nn.Conv1d(in_channels=3, out_channels=4, kernel_size=2, padding=1, dilation=2), torch.rand(2, 3, 4)),
        (nn.Conv2d(in_channels=3, out_channels=4, kernel_size=2, padding=1, dilation=2), torch.rand(2, 3, 4, 4)),
        (nn.Conv3d(in_channels=3, out_channels=4, kernel_size=2, padding=1, dilation=2), torch.rand(2, 3, 4, 4, 4)),
        (nn.ConvTranspose1d(in_channels=3, out_channels=4, kernel_size=2, padding=1, dilation=2), torch.rand(2, 3, 4)),
22
23
24
25
26
27
28
29
        (
            nn.ConvTranspose2d(in_channels=3, out_channels=4, kernel_size=2, padding=1, dilation=2),
            torch.rand(2, 3, 4, 4),
        ),
        (
            nn.ConvTranspose3d(in_channels=3, out_channels=4, kernel_size=2, padding=1, dilation=2),
            torch.rand(2, 3, 4, 4, 4),
        ),
30
    ],
31
    ("aten.native_batch_norm.default", True): [
32
33
34
35
        (nn.BatchNorm1d(4), torch.rand(2, 4)),
        (nn.BatchNorm2d(4), torch.rand(1, 4, 4, 4)),
        (nn.BatchNorm3d(4), torch.rand(1, 4, 4, 4, 4)),
    ],
36
37
38
39
    ("aten.native_layer_norm.default", True): [
        (nn.LayerNorm(4), torch.rand(1, 2, 3, 4)),
    ],
    ("aten.avg_pool1d.default", True): [
40
41
42
43
44
        (nn.MaxPool1d(3, stride=2), torch.rand(4, 5, 5)),
        (nn.AvgPool1d(3, stride=2), torch.rand(4, 5, 5)),
        (nn.AdaptiveMaxPool1d(3), torch.rand(4, 5, 5)),
        (nn.AdaptiveAvgPool1d(3), torch.rand(4, 5, 5)),
    ],
45
    ("aten.avg_pool2d.default", True): [
46
47
48
49
50
        (nn.MaxPool2d((3, 2), stride=(2, 1)), torch.rand(2, 4, 5, 5)),
        (nn.AvgPool2d((3, 2), stride=(2, 1)), torch.rand(2, 4, 5, 5)),
        (nn.AdaptiveMaxPool2d((3, 2)), torch.rand(2, 4, 5, 5)),
        (nn.AdaptiveAvgPool2d((3, 2)), torch.rand(2, 4, 5, 5)),
    ],
51
    ("aten.relu.default", True): [
52
53
54
55
56
57
58
59
        (nn.ReLU(), torch.rand(4, 3, 1, 2)),
        (nn.LeakyReLU(), torch.rand(4, 3, 1, 2)),
        (nn.SiLU(), torch.rand(4, 3, 1, 2)),
        (nn.GELU(), torch.rand(4, 3, 1, 2)),
        (nn.ELU(), torch.rand(4, 3, 1, 2)),
        (nn.Sigmoid(), torch.rand(4, 3, 1, 2)),
        (nn.Tanh(), torch.rand(4, 3, 1, 2)),
        (nn.Hardswish(), torch.rand(4, 3, 1, 2)),
60
    ],
61
62
63
64
}


def compare_all(tensor: torch.Tensor, meta_tensor: torch.Tensor) -> Any:
65
66
67
68
69
70
71
72
73
    assert (
        tensor.shape == meta_tensor.shape
    ), f"the shape of tensor ({tensor.shape}) and meta tensor ({meta_tensor.shape}) does not match."
    assert (
        tensor.dtype == meta_tensor.dtype
    ), f"the dtype of tensor ({tensor.dtype}) and meta tensor ({meta_tensor.dtype}) does not match."
    assert (
        tensor.stride() == meta_tensor.stride()
    ), f"the stride of tensor ({tensor.stride()}) and meta tensor ({meta_tensor.stride()}) does not match."
74
75
76
77
78
79
80
81
82
83
84
85
86


def run_and_compare(f: Union[nn.Module, Callable], x: torch.Tensor, requires_backward=False) -> Any:
    x.requires_grad = requires_backward
    meta_x = MetaTensor(x)
    x_out, meta_out = f(x), f(meta_x)
    compare_all(x_out, meta_out)
    if requires_backward:
        x_out.sum().backward()
        meta_out.sum().backward()
        compare_all(x.grad, meta_x.grad)


87
@pytest.mark.skipif(torch.__version__ < "1.12.0", reason="torch version < 12")
88
@clear_cache_before_run()
89
90
91
92
93
94
def test_meta_aten():
    for (aten_op, requires_backward), v in registered_meta.items():
        for f, x in v:
            run_and_compare(f, x, requires_backward)


95
if __name__ == "__main__":
96
    test_meta_aten()