|
| 1 | +import functools |
| 2 | + |
| 3 | +import e3nn |
| 4 | +import pytest |
| 5 | +import torch |
| 6 | +from e3nn import o3 |
| 7 | +from e3tools.nn import ( |
| 8 | + Attention, |
| 9 | + Conv, |
| 10 | + ConvBlock, |
| 11 | + EquivariantMLP, |
| 12 | + ExperimentalConv, |
| 13 | + Gated, |
| 14 | + LayerNorm, |
| 15 | + MultiheadAttention, |
| 16 | + SeparableConv, |
| 17 | + TransformerBlock, |
| 18 | +) |
| 19 | +from e3tools import radius_graph |
| 20 | + |
| 21 | +torch.set_default_dtype(torch.float64) |
| 22 | + |
| 23 | +CONV_LAYERS = [Conv, SeparableConv, ExperimentalConv] |
| 24 | + |
| 25 | + |
| 26 | +def apply_layer_rot(layer): |
| 27 | + N = 20 |
| 28 | + edge_attr_dim = 10 |
| 29 | + max_radius = 1.3 |
| 30 | + |
| 31 | + pos = torch.randn(N, 3) |
| 32 | + node_attr = layer.irreps_in.randn(N, -1) |
| 33 | + |
| 34 | + edge_index = radius_graph(pos, max_radius) |
| 35 | + edge_vec = pos[edge_index[0]] - pos[edge_index[1]] |
| 36 | + edge_length = (edge_vec).norm(dim=1) |
| 37 | + edge_attr = e3nn.math.soft_one_hot_linspace( |
| 38 | + edge_length, |
| 39 | + start=0.0, |
| 40 | + end=max_radius, |
| 41 | + number=edge_attr_dim, |
| 42 | + basis="smooth_finite", |
| 43 | + cutoff=True, |
| 44 | + ) |
| 45 | + |
| 46 | + edge_sh = o3.spherical_harmonics( |
| 47 | + layer.irreps_sh, edge_vec, True, normalization="component" |
| 48 | + ) |
| 49 | + |
| 50 | + rot = o3.rand_matrix() |
| 51 | + |
| 52 | + D_node_attr = layer.irreps_in.D_from_matrix(rot) |
| 53 | + D_edge_sh = layer.irreps_sh.D_from_matrix(rot) |
| 54 | + |
| 55 | + D_out = layer.irreps_out.D_from_matrix(rot) |
| 56 | + |
| 57 | + out_1 = layer( |
| 58 | + node_attr @ D_node_attr.T, edge_index, edge_attr, edge_sh @ D_edge_sh.T |
| 59 | + ) |
| 60 | + out_2 = layer(node_attr, edge_index, edge_attr, edge_sh) @ D_out.T |
| 61 | + |
| 62 | + return out_1, out_2 |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize("conv", CONV_LAYERS) |
| 66 | +def test_conv(conv): |
| 67 | + irreps_in = o3.Irreps("10x0e + 10x1o + 10x2e") |
| 68 | + irreps_sh = irreps_in.spherical_harmonics(2) |
| 69 | + edge_attr_dim = 10 |
| 70 | + |
| 71 | + layer = conv(irreps_in, irreps_in, irreps_sh, edge_attr_dim=edge_attr_dim) |
| 72 | + |
| 73 | + out_1, out_2 = apply_layer_rot(layer) |
| 74 | + assert torch.allclose(out_1, out_2, atol=1e-10) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize("conv", CONV_LAYERS) |
| 78 | +def test_gated_conv(conv): |
| 79 | + irreps_in = o3.Irreps("10x0e + 10x1o + 10x2e") |
| 80 | + irreps_sh = irreps_in.spherical_harmonics(2) |
| 81 | + edge_attr_dim = 10 |
| 82 | + |
| 83 | + wrapped = functools.partial(conv, irreps_sh=irreps_sh, edge_attr_dim=edge_attr_dim) |
| 84 | + |
| 85 | + layer = Gated(wrapped, irreps_in=irreps_in, irreps_out=irreps_in) |
| 86 | + |
| 87 | + out_1, out_2 = apply_layer_rot(layer) |
| 88 | + assert torch.allclose(out_1, out_2, atol=1e-10) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize("conv", CONV_LAYERS) |
| 92 | +def test_conv_block(conv): |
| 93 | + irreps_in = o3.Irreps("10x0e + 10x1o + 10x2e") |
| 94 | + irreps_sh = irreps_in.spherical_harmonics(2) |
| 95 | + edge_attr_dim = 10 |
| 96 | + |
| 97 | + layer = ConvBlock( |
| 98 | + irreps_in=irreps_in, |
| 99 | + irreps_out=irreps_in, |
| 100 | + irreps_sh=irreps_sh, |
| 101 | + edge_attr_dim=edge_attr_dim, |
| 102 | + conv=conv, |
| 103 | + ) |
| 104 | + |
| 105 | + out_1, out_2 = apply_layer_rot(layer) |
| 106 | + assert torch.allclose(out_1, out_2, atol=1e-10) |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.parametrize("conv", CONV_LAYERS) |
| 110 | +def test_attention(conv): |
| 111 | + irreps_in = o3.Irreps("10x0e + 10x1o + 10x2e") |
| 112 | + irreps_out = irreps_in |
| 113 | + irreps_sh = irreps_in.spherical_harmonics(2) |
| 114 | + irreps_key = irreps_in |
| 115 | + irreps_query = irreps_in |
| 116 | + edge_attr_dim = 10 |
| 117 | + |
| 118 | + layer = Attention( |
| 119 | + irreps_in, |
| 120 | + irreps_out, |
| 121 | + irreps_sh, |
| 122 | + irreps_query, |
| 123 | + irreps_key, |
| 124 | + edge_attr_dim, |
| 125 | + conv=conv, |
| 126 | + ) |
| 127 | + |
| 128 | + out_1, out_2 = apply_layer_rot(layer) |
| 129 | + assert torch.allclose(out_1, out_2, atol=1e-10) |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.parametrize("conv", [Conv, SeparableConv]) |
| 133 | +def test_multihead_attention(conv): |
| 134 | + irreps_in = o3.Irreps("10x0e + 10x1o + 10x2e") |
| 135 | + irreps_out = irreps_in |
| 136 | + irreps_sh = irreps_in.spherical_harmonics(2) |
| 137 | + irreps_key = irreps_in |
| 138 | + irreps_query = irreps_in |
| 139 | + edge_attr_dim = 10 |
| 140 | + n_head = 2 |
| 141 | + |
| 142 | + layer = MultiheadAttention( |
| 143 | + irreps_in, |
| 144 | + irreps_out, |
| 145 | + irreps_sh, |
| 146 | + irreps_query, |
| 147 | + irreps_key, |
| 148 | + edge_attr_dim, |
| 149 | + n_head, |
| 150 | + conv=conv, |
| 151 | + ) |
| 152 | + |
| 153 | + out_1, out_2 = apply_layer_rot(layer) |
| 154 | + assert torch.allclose(out_1, out_2, atol=1e-10) |
| 155 | + |
| 156 | + |
| 157 | +def test_layer_norm(): |
| 158 | + irreps = o3.Irreps("10x0e + 10x1o + 10x2e") |
| 159 | + |
| 160 | + layer = LayerNorm(irreps) |
| 161 | + rot = o3.rand_matrix() |
| 162 | + D = irreps.D_from_matrix(rot) |
| 163 | + |
| 164 | + x = irreps.randn(10, -1) |
| 165 | + |
| 166 | + out_1 = layer(x @ D.T) |
| 167 | + out_2 = layer(x) @ D.T |
| 168 | + |
| 169 | + assert torch.allclose(out_1, out_2, atol=1e-10) |
| 170 | + |
| 171 | + |
| 172 | +def test_equivariant_mlp(): |
| 173 | + irreps = o3.Irreps("10x0e + 10x1o + 10x2e") |
| 174 | + irreps_hidden = o3.Irreps([(4 * mul, ir) for mul, ir in irreps]) |
| 175 | + |
| 176 | + layer = EquivariantMLP( |
| 177 | + irreps, irreps, [irreps_hidden, irreps_hidden], norm_layer=LayerNorm |
| 178 | + ) |
| 179 | + |
| 180 | + rot = o3.rand_matrix() |
| 181 | + D = irreps.D_from_matrix(rot) |
| 182 | + |
| 183 | + x = irreps.randn(10, -1) |
| 184 | + |
| 185 | + out_1 = layer(x @ D.T) |
| 186 | + out_2 = layer(x) @ D.T |
| 187 | + |
| 188 | + assert torch.allclose(out_1, out_2, atol=1e-10) |
| 189 | + |
| 190 | + |
| 191 | +def test_transformer(): |
| 192 | + irreps_in = o3.Irreps("10x0e + 10x1o + 10x2e") |
| 193 | + irreps_out = irreps_in |
| 194 | + irreps_sh = irreps_in.spherical_harmonics(2) |
| 195 | + edge_attr_dim = 10 |
| 196 | + n_head = 2 |
| 197 | + |
| 198 | + layer = TransformerBlock( |
| 199 | + irreps_in=irreps_in, |
| 200 | + irreps_out=irreps_out, |
| 201 | + irreps_sh=irreps_sh, |
| 202 | + edge_attr_dim=edge_attr_dim, |
| 203 | + n_head=n_head, |
| 204 | + ) |
| 205 | + |
| 206 | + out_1, out_2 = apply_layer_rot(layer) |
| 207 | + assert torch.allclose(out_1, out_2, atol=1e-10) |
0 commit comments