lhallee commited on
Commit
2833e41
·
verified ·
1 Parent(s): a9244b3

Upload vb_layers_triangular_mult.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. vb_layers_triangular_mult.py +215 -215
vb_layers_triangular_mult.py CHANGED
@@ -1,215 +1,215 @@
1
- import importlib
2
-
3
- import torch
4
- from torch import Tensor, nn
5
-
6
- from . import vb_layers_initialize as init
7
-
8
-
9
- @torch.compiler.disable
10
- def kernel_triangular_mult(
11
- x,
12
- direction,
13
- mask,
14
- norm_in_weight,
15
- norm_in_bias,
16
- p_in_weight,
17
- g_in_weight,
18
- norm_out_weight,
19
- norm_out_bias,
20
- p_out_weight,
21
- g_out_weight,
22
- eps,
23
- ):
24
- triangle_module = importlib.import_module("cuequivariance_torch.primitives.triangle")
25
- triangle_multiplicative_update = triangle_module.triangle_multiplicative_update
26
- return triangle_multiplicative_update(
27
- x,
28
- direction=direction,
29
- mask=mask,
30
- norm_in_weight=norm_in_weight,
31
- norm_in_bias=norm_in_bias,
32
- p_in_weight=p_in_weight,
33
- g_in_weight=g_in_weight,
34
- norm_out_weight=norm_out_weight,
35
- norm_out_bias=norm_out_bias,
36
- p_out_weight=p_out_weight,
37
- g_out_weight=g_out_weight,
38
- eps=eps,
39
- )
40
-
41
-
42
- class TriangleMultiplicationOutgoing(nn.Module):
43
- """TriangleMultiplicationOutgoing."""
44
-
45
- def __init__(self, dim: int = 128) -> None:
46
- """Initialize the TriangularUpdate module.
47
-
48
- Parameters
49
- ----------
50
- dim: int
51
- The dimension of the input, default 128
52
-
53
- """
54
- super().__init__()
55
-
56
- self.norm_in = nn.LayerNorm(dim, eps=1e-5)
57
- self.p_in = nn.Linear(dim, 2 * dim, bias=False)
58
- self.g_in = nn.Linear(dim, 2 * dim, bias=False)
59
-
60
- self.norm_out = nn.LayerNorm(dim)
61
- self.p_out = nn.Linear(dim, dim, bias=False)
62
- self.g_out = nn.Linear(dim, dim, bias=False)
63
-
64
- init.bias_init_one_(self.norm_in.weight)
65
- init.bias_init_zero_(self.norm_in.bias)
66
-
67
- init.lecun_normal_init_(self.p_in.weight)
68
- init.gating_init_(self.g_in.weight)
69
-
70
- init.bias_init_one_(self.norm_out.weight)
71
- init.bias_init_zero_(self.norm_out.bias)
72
-
73
- init.final_init_(self.p_out.weight)
74
- init.gating_init_(self.g_out.weight)
75
-
76
- def forward(self, x: Tensor, mask: Tensor, use_kernels: bool = False) -> Tensor:
77
- """Perform a forward pass.
78
-
79
- Parameters
80
- ----------
81
- x: torch.Tensor
82
- The input data of shape (B, N, N, D)
83
- mask: torch.Tensor
84
- The input mask of shape (B, N, N)
85
- use_kernels: bool
86
- Whether to use the kernel
87
-
88
- Returns
89
- -------
90
- x: torch.Tensor
91
- The output data of shape (B, N, N, D)
92
-
93
- """
94
- if use_kernels:
95
- return kernel_triangular_mult(
96
- x,
97
- direction="outgoing",
98
- mask=mask,
99
- norm_in_weight=self.norm_in.weight,
100
- norm_in_bias=self.norm_in.bias,
101
- p_in_weight=self.p_in.weight,
102
- g_in_weight=self.g_in.weight,
103
- norm_out_weight=self.norm_out.weight,
104
- norm_out_bias=self.norm_out.bias,
105
- p_out_weight=self.p_out.weight,
106
- g_out_weight=self.g_out.weight,
107
- eps=1e-5,
108
- )
109
-
110
- # Input gating: D -> D
111
- x = self.norm_in(x)
112
- x_in = x
113
- x = self.p_in(x) * self.g_in(x).sigmoid()
114
-
115
- # Apply mask
116
- x = x * mask.unsqueeze(-1)
117
-
118
- # Split input and cast to float
119
- a, b = torch.chunk(x.float(), 2, dim=-1)
120
-
121
- # Triangular projection
122
- x = torch.einsum("bikd,bjkd->bijd", a, b)
123
-
124
- # Output gating
125
- x = self.p_out(self.norm_out(x)) * self.g_out(x_in).sigmoid()
126
-
127
- return x
128
-
129
-
130
- class TriangleMultiplicationIncoming(nn.Module):
131
- """TriangleMultiplicationIncoming."""
132
-
133
- def __init__(self, dim: int = 128) -> None:
134
- """Initialize the TriangularUpdate module.
135
-
136
- Parameters
137
- ----------
138
- dim: int
139
- The dimension of the input, default 128
140
-
141
- """
142
- super().__init__()
143
-
144
- self.norm_in = nn.LayerNorm(dim, eps=1e-5)
145
- self.p_in = nn.Linear(dim, 2 * dim, bias=False)
146
- self.g_in = nn.Linear(dim, 2 * dim, bias=False)
147
-
148
- self.norm_out = nn.LayerNorm(dim)
149
- self.p_out = nn.Linear(dim, dim, bias=False)
150
- self.g_out = nn.Linear(dim, dim, bias=False)
151
-
152
- init.bias_init_one_(self.norm_in.weight)
153
- init.bias_init_zero_(self.norm_in.bias)
154
-
155
- init.lecun_normal_init_(self.p_in.weight)
156
- init.gating_init_(self.g_in.weight)
157
-
158
- init.bias_init_one_(self.norm_out.weight)
159
- init.bias_init_zero_(self.norm_out.bias)
160
-
161
- init.final_init_(self.p_out.weight)
162
- init.gating_init_(self.g_out.weight)
163
-
164
- def forward(self, x: Tensor, mask: Tensor, use_kernels: bool = False) -> Tensor:
165
- """Perform a forward pass.
166
-
167
- Parameters
168
- ----------
169
- x: torch.Tensor
170
- The input data of shape (B, N, N, D)
171
- mask: torch.Tensor
172
- The input mask of shape (B, N, N)
173
- use_kernels: bool
174
- Whether to use the kernel
175
-
176
- Returns
177
- -------
178
- x: torch.Tensor
179
- The output data of shape (B, N, N, D)
180
-
181
- """
182
- if use_kernels:
183
- return kernel_triangular_mult(
184
- x,
185
- direction="incoming",
186
- mask=mask,
187
- norm_in_weight=self.norm_in.weight,
188
- norm_in_bias=self.norm_in.bias,
189
- p_in_weight=self.p_in.weight,
190
- g_in_weight=self.g_in.weight,
191
- norm_out_weight=self.norm_out.weight,
192
- norm_out_bias=self.norm_out.bias,
193
- p_out_weight=self.p_out.weight,
194
- g_out_weight=self.g_out.weight,
195
- eps=1e-5,
196
- )
197
-
198
- # Input gating: D -> D
199
- x = self.norm_in(x)
200
- x_in = x
201
- x = self.p_in(x) * self.g_in(x).sigmoid()
202
-
203
- # Apply mask
204
- x = x * mask.unsqueeze(-1)
205
-
206
- # Split input and cast to float
207
- a, b = torch.chunk(x.float(), 2, dim=-1)
208
-
209
- # Triangular projection
210
- x = torch.einsum("bkid,bkjd->bijd", a, b)
211
-
212
- # Output gating
213
- x = self.p_out(self.norm_out(x)) * self.g_out(x_in).sigmoid()
214
-
215
- return x
 
1
+ import importlib
2
+
3
+ import torch
4
+ from torch import Tensor, nn
5
+
6
+ from . import vb_layers_initialize as init
7
+
8
+
9
+ @torch.compiler.disable
10
+ def kernel_triangular_mult(
11
+ x,
12
+ direction,
13
+ mask,
14
+ norm_in_weight,
15
+ norm_in_bias,
16
+ p_in_weight,
17
+ g_in_weight,
18
+ norm_out_weight,
19
+ norm_out_bias,
20
+ p_out_weight,
21
+ g_out_weight,
22
+ eps,
23
+ ):
24
+ triangle_module = importlib.import_module("cuequivariance_torch.primitives.triangle")
25
+ triangle_multiplicative_update = triangle_module.triangle_multiplicative_update
26
+ return triangle_multiplicative_update(
27
+ x,
28
+ direction=direction,
29
+ mask=mask,
30
+ norm_in_weight=norm_in_weight,
31
+ norm_in_bias=norm_in_bias,
32
+ p_in_weight=p_in_weight,
33
+ g_in_weight=g_in_weight,
34
+ norm_out_weight=norm_out_weight,
35
+ norm_out_bias=norm_out_bias,
36
+ p_out_weight=p_out_weight,
37
+ g_out_weight=g_out_weight,
38
+ eps=eps,
39
+ )
40
+
41
+
42
+ class TriangleMultiplicationOutgoing(nn.Module):
43
+ """TriangleMultiplicationOutgoing."""
44
+
45
+ def __init__(self, dim: int = 128) -> None:
46
+ """Initialize the TriangularUpdate module.
47
+
48
+ Parameters
49
+ ----------
50
+ dim: int
51
+ The dimension of the input, default 128
52
+
53
+ """
54
+ super().__init__()
55
+
56
+ self.norm_in = nn.LayerNorm(dim, eps=1e-5)
57
+ self.p_in = nn.Linear(dim, 2 * dim, bias=False)
58
+ self.g_in = nn.Linear(dim, 2 * dim, bias=False)
59
+
60
+ self.norm_out = nn.LayerNorm(dim)
61
+ self.p_out = nn.Linear(dim, dim, bias=False)
62
+ self.g_out = nn.Linear(dim, dim, bias=False)
63
+
64
+ init.bias_init_one_(self.norm_in.weight)
65
+ init.bias_init_zero_(self.norm_in.bias)
66
+
67
+ init.lecun_normal_init_(self.p_in.weight)
68
+ init.gating_init_(self.g_in.weight)
69
+
70
+ init.bias_init_one_(self.norm_out.weight)
71
+ init.bias_init_zero_(self.norm_out.bias)
72
+
73
+ init.final_init_(self.p_out.weight)
74
+ init.gating_init_(self.g_out.weight)
75
+
76
+ def forward(self, x: Tensor, mask: Tensor, use_kernels: bool = False) -> Tensor:
77
+ """Perform a forward pass.
78
+
79
+ Parameters
80
+ ----------
81
+ x: torch.Tensor
82
+ The input data of shape (B, N, N, D)
83
+ mask: torch.Tensor
84
+ The input mask of shape (B, N, N)
85
+ use_kernels: bool
86
+ Whether to use the kernel
87
+
88
+ Returns
89
+ -------
90
+ x: torch.Tensor
91
+ The output data of shape (B, N, N, D)
92
+
93
+ """
94
+ if use_kernels:
95
+ return kernel_triangular_mult(
96
+ x,
97
+ direction="outgoing",
98
+ mask=mask,
99
+ norm_in_weight=self.norm_in.weight,
100
+ norm_in_bias=self.norm_in.bias,
101
+ p_in_weight=self.p_in.weight,
102
+ g_in_weight=self.g_in.weight,
103
+ norm_out_weight=self.norm_out.weight,
104
+ norm_out_bias=self.norm_out.bias,
105
+ p_out_weight=self.p_out.weight,
106
+ g_out_weight=self.g_out.weight,
107
+ eps=1e-5,
108
+ )
109
+
110
+ # Input gating: D -> D
111
+ x = self.norm_in(x)
112
+ x_in = x
113
+ x = self.p_in(x) * self.g_in(x).sigmoid()
114
+
115
+ # Apply mask
116
+ x = x * mask.unsqueeze(-1)
117
+
118
+ # Split input and cast to float
119
+ a, b = torch.chunk(x.float(), 2, dim=-1)
120
+
121
+ # Triangular projection
122
+ x = torch.einsum("bikd,bjkd->bijd", a, b)
123
+
124
+ # Output gating
125
+ x = self.p_out(self.norm_out(x)) * self.g_out(x_in).sigmoid()
126
+
127
+ return x
128
+
129
+
130
+ class TriangleMultiplicationIncoming(nn.Module):
131
+ """TriangleMultiplicationIncoming."""
132
+
133
+ def __init__(self, dim: int = 128) -> None:
134
+ """Initialize the TriangularUpdate module.
135
+
136
+ Parameters
137
+ ----------
138
+ dim: int
139
+ The dimension of the input, default 128
140
+
141
+ """
142
+ super().__init__()
143
+
144
+ self.norm_in = nn.LayerNorm(dim, eps=1e-5)
145
+ self.p_in = nn.Linear(dim, 2 * dim, bias=False)
146
+ self.g_in = nn.Linear(dim, 2 * dim, bias=False)
147
+
148
+ self.norm_out = nn.LayerNorm(dim)
149
+ self.p_out = nn.Linear(dim, dim, bias=False)
150
+ self.g_out = nn.Linear(dim, dim, bias=False)
151
+
152
+ init.bias_init_one_(self.norm_in.weight)
153
+ init.bias_init_zero_(self.norm_in.bias)
154
+
155
+ init.lecun_normal_init_(self.p_in.weight)
156
+ init.gating_init_(self.g_in.weight)
157
+
158
+ init.bias_init_one_(self.norm_out.weight)
159
+ init.bias_init_zero_(self.norm_out.bias)
160
+
161
+ init.final_init_(self.p_out.weight)
162
+ init.gating_init_(self.g_out.weight)
163
+
164
+ def forward(self, x: Tensor, mask: Tensor, use_kernels: bool = False) -> Tensor:
165
+ """Perform a forward pass.
166
+
167
+ Parameters
168
+ ----------
169
+ x: torch.Tensor
170
+ The input data of shape (B, N, N, D)
171
+ mask: torch.Tensor
172
+ The input mask of shape (B, N, N)
173
+ use_kernels: bool
174
+ Whether to use the kernel
175
+
176
+ Returns
177
+ -------
178
+ x: torch.Tensor
179
+ The output data of shape (B, N, N, D)
180
+
181
+ """
182
+ if use_kernels:
183
+ return kernel_triangular_mult(
184
+ x,
185
+ direction="incoming",
186
+ mask=mask,
187
+ norm_in_weight=self.norm_in.weight,
188
+ norm_in_bias=self.norm_in.bias,
189
+ p_in_weight=self.p_in.weight,
190
+ g_in_weight=self.g_in.weight,
191
+ norm_out_weight=self.norm_out.weight,
192
+ norm_out_bias=self.norm_out.bias,
193
+ p_out_weight=self.p_out.weight,
194
+ g_out_weight=self.g_out.weight,
195
+ eps=1e-5,
196
+ )
197
+
198
+ # Input gating: D -> D
199
+ x = self.norm_in(x)
200
+ x_in = x
201
+ x = self.p_in(x) * self.g_in(x).sigmoid()
202
+
203
+ # Apply mask
204
+ x = x * mask.unsqueeze(-1)
205
+
206
+ # Split input and cast to float
207
+ a, b = torch.chunk(x.float(), 2, dim=-1)
208
+
209
+ # Triangular projection
210
+ x = torch.einsum("bkid,bkjd->bijd", a, b)
211
+
212
+ # Output gating
213
+ x = self.p_out(self.norm_out(x)) * self.g_out(x_in).sigmoid()
214
+
215
+ return x