File size: 20,422 Bytes
8f2863d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import torch
import torch.nn as nn
import torch.nn.functional as F
import pdb

class IntraGraphAttention(nn.Module):
    def __init__(self, d_node, d_edge, num_heads, negative_slope=0.2):
        super(IntraGraphAttention, self).__init__()
        assert d_node % num_heads == 0, "d_node must be divisible by num_heads"
        assert d_edge % num_heads == 0, "d_edge must be divisible by num_heads"

        self.num_heads = num_heads
        self.d_k = d_node // num_heads
        self.d_edge_head = d_edge // num_heads

        self.Wn = nn.Linear(d_node, d_node)
        self.Wh = nn.Linear(self.d_k, self.d_k)
        self.We = nn.Linear(d_edge, d_edge)
        self.Wn_2 = nn.Linear(d_node, d_node)
        self.We_2 = nn.Linear(d_edge, d_edge)
        self.attn_linear = nn.Linear(self.d_k * 2 + self.d_edge_head, 1, bias=False)
        self.edge_linear = nn.Linear(self.d_k * 2 + self.d_edge_head, self.d_edge_head)

        self.out_proj_node = nn.Linear(d_node, d_node)
        self.out_proj_edge = nn.Linear(d_edge, d_edge)

        self.leaky_relu = nn.LeakyReLU(negative_slope)

    def forward(self, node_representation, edge_representation):
        # node_representation: (B, L, d_node)
        # edge_representation: (B, L, L, d_edge)
        # pdb.set_trace()
        B, L, d_node = node_representation.size()
        d_edge = edge_representation.size(-1)
        
        # Multi-head projection
        node_proj = self.Wn(node_representation).view(B, L, self.num_heads, self.d_k)  # (B, L, num_heads, d_k)
        edge_proj = self.We(edge_representation).view(B, L, L, self.num_heads, self.d_edge_head)  # (B, L, L, num_heads, d_edge_head)
        
        # Node representation update
        new_node_representation = self.single_head_attention_node(node_proj, edge_proj)
        
        concatenated_node_rep = new_node_representation.view(B, L, -1)  # Shape: (B, L, num_heads * d_k)
        new_node_representation = self.out_proj_node(concatenated_node_rep)

        # Edge representation update
        node_proj_2 = self.Wn_2(new_node_representation).view(B, L, self.num_heads, self.d_k)  # (B, L, num_heads, d_k)
        edge_proj_2 = self.We_2(edge_representation).view(B, L, L, self.num_heads, self.d_edge_head)  # (B, L, L, num_heads, d_edge_head)
        
        new_edge_representation = self.single_head_attention_edge(node_proj_2, edge_proj_2)
        
        concatenated_edge_rep = new_edge_representation.view(B, L, L, -1)  # Shape: (B, L, L, num_heads * d_edge_head)
        new_edge_representation = self.out_proj_edge(concatenated_edge_rep)
        
        return new_node_representation, new_edge_representation

    def single_head_attention_node(self, node_representation, edge_representation):
        B, L, num_heads, d_k = node_representation.size()
        d_edge_head = edge_representation.size(-1)

        hi = node_representation.unsqueeze(2)  # shape: (B, L, 1, num_heads, d_k)
        hj = node_representation.unsqueeze(1)  # shape: (B, 1, L, num_heads, d_k)

        hi_hj_concat = torch.cat([hi.expand(-1, -1, L, -1, -1), 
                                hj.expand(-1, L, -1, -1, -1), 
                                edge_representation], dim=-1)  # shape: (B, L, L, num_heads, 2*d_k + d_edge_head)

        attention_scores = self.attn_linear(hi_hj_concat).squeeze(-1)  # shape: (B, L, L, num_heads)
        
        # Mask the diagonal (self-attention) by setting it to a large negative value
        mask = torch.eye(L).bool().unsqueeze(0).unsqueeze(-1).to(node_representation.device)  # shape: (1, L, L, 1)
        attention_scores.masked_fill_(mask, float('-inf'))
        
        attention_probs = F.softmax(self.leaky_relu(attention_scores), dim=2)  # shape: (B, L, L, num_heads)

        # Aggregating features correctly along the L dimension
        node_representation_Wh = self.Wh(node_representation)  # shape: (B, L, num_heads, d_k)
        node_representation_Wh = node_representation_Wh.permute(0, 2, 1, 3)  # shape: (B, num_heads, L, d_k)
        
        aggregated_features = torch.matmul(attention_probs.permute(0, 3, 1, 2), node_representation_Wh)  # shape: (B, num_heads, L, d_k)
        aggregated_features = aggregated_features.permute(0, 2, 1, 3)  # shape: (B, L, num_heads, d_k)

        new_node_representation = node_representation + self.leaky_relu(aggregated_features)  # shape: (B, L, num_heads, d_k)

        return new_node_representation

    def single_head_attention_edge(self, node_representation, edge_representation):
        # Update edge representation
        B, L, num_heads, d_k = node_representation.size()
        d_edge_head = edge_representation.size(-1)

        hi = node_representation.unsqueeze(2)  # shape: (B, L, 1, num_heads, d_k)
        hj = node_representation.unsqueeze(1)  # shape: (B, 1, L, num_heads, d_k)

        hi_hj_concat = torch.cat([edge_representation, hi.expand(-1, -1, L, -1, -1), hj.expand(-1, L, -1, -1, -1)], dim=-1)  # shape: (B, L, L, num_heads, 2*d_k + d_edge_head)

        new_edge_representation = self.edge_linear(hi_hj_concat)  # shape: (B, L, L, num_heads, d_edge_head)

        return new_edge_representation


class DiffEmbeddingLayer(nn.Module):
    def __init__(self, d_node):
        super(DiffEmbeddingLayer, self).__init__()
        self.W_delta = nn.Linear(d_node, d_node)

    def forward(self, wt_node, mut_node):
        delta_h = mut_node - wt_node  # (B, L, d_node)
        diff_vec = torch.relu(self.W_delta(delta_h))  # (B, L, d_node)
        return diff_vec


class MIM(nn.Module):
    def __init__(self, d_node, d_edge, d_diff, num_heads, negative_slope=0.2):
        super(MIM, self).__init__()
        assert d_node % num_heads == 0, "d_node must be divisible by num_heads"
        assert d_edge % num_heads == 0, "d_edge must be divisible by num_heads"
        assert d_diff % num_heads == 0, "d_diff must be divisible by num_heads"

        self.num_heads = num_heads
        self.d_k = d_node // num_heads
        self.d_edge_head = d_edge // num_heads
        self.d_diff_head = d_diff // num_heads

        self.Wn = nn.Linear(d_node, d_node)
        self.Wh = nn.Linear(self.d_k, self.d_k)
        self.We = nn.Linear(d_edge, d_edge)
        self.Wn_2 = nn.Linear(d_node, d_node)
        self.We_2 = nn.Linear(d_edge, d_edge)
        self.Wd = nn.Linear(d_diff, d_diff)
        self.Wd_2 = nn.Linear(d_diff, d_diff)
        self.attn_linear = nn.Linear(self.d_k * 2 + self.d_edge_head + 2 * self.d_diff_head, 1, bias=False)
        self.edge_linear = nn.Linear(self.d_k * 2 + self.d_edge_head + 2 * self.d_diff_head, self.d_edge_head)

        self.out_proj_node = nn.Linear(d_node, d_node)
        self.out_proj_edge = nn.Linear(d_edge, d_edge)

        self.leaky_relu = nn.LeakyReLU(negative_slope)

    def forward(self, node_representation, edge_representation, diff_vec):
        # node_representation: (B, L, d_node)
        # edge_representation: (B, L, L, d_edge)
        # diff_vec: (B, L, d_diff)

        B, L, d_node = node_representation.size()
        d_edge = edge_representation.size(-1)
        
        # Multi-head projection
        node_proj = self.Wn(node_representation).view(B, L, self.num_heads, self.d_k)  # (B, L, num_heads, d_k)
        edge_proj = self.We(edge_representation).view(B, L, L, self.num_heads, self.d_edge_head)  # (B, L, L, num_heads, d_edge_head)
        diff_proj = self.Wd(diff_vec).view(B, L, self.num_heads, self.d_diff_head)  # (B, L, num_heads, d_diff_head)

        # Node representation update
        new_node_representation = self.single_head_attention_node(node_proj, edge_proj, diff_proj)
        
        concatenated_node_rep = new_node_representation.view(B, L, -1)  # Shape: (B, L, num_heads * d_k)
        new_node_representation = self.out_proj_node(concatenated_node_rep)

        # Edge representation update
        node_proj_2 = self.Wn_2(new_node_representation).view(B, L, self.num_heads, self.d_k)  # (B, L, num_heads, d_k)
        edge_proj_2 = self.We_2(edge_representation).view(B, L, L, self.num_heads, self.d_edge_head)  # (B, L, L, num_heads, d_edge_head)
        diff_proj_2 = self.Wd_2(diff_vec).view(B, L, self.num_heads, self.d_diff_head)  # (B, L, num_heads, d_diff_head)

        new_edge_representation = self.single_head_attention_edge(node_proj_2, edge_proj_2, diff_proj_2)
        
        concatenated_edge_rep = new_edge_representation.view(B, L, L, -1)  # Shape: (B, L, L, num_heads * d_edge_head)
        new_edge_representation = self.out_proj_edge(concatenated_edge_rep)
        
        return new_node_representation, new_edge_representation

    def single_head_attention_node(self, node_representation, edge_representation, diff_vec):
        # Update node representation
        B, L, num_heads, d_k = node_representation.size()
        d_edge_head = edge_representation.size(-1)
        d_diff_head = diff_vec.size(-1)

        hi = node_representation.unsqueeze(2)  # shape: (B, L, 1, num_heads, d_k)
        hj = node_representation.unsqueeze(1)  # shape: (B, 1, L, num_heads, d_k)
        diff_i = diff_vec.unsqueeze(2)  # shape: (B, L, 1, num_heads, d_diff_head)
        diff_j = diff_vec.unsqueeze(1)  # shape: (B, 1, L, num_heads, d_diff_head)
        
        hi_hj_concat = torch.cat([
            hi.expand(-1, -1, L, -1, -1), 
            hj.expand(-1, L, -1, -1, -1), 
            edge_representation, 
            diff_i.expand(-1, -1, L, -1, -1), 
            diff_j.expand(-1, L, -1, -1, -1)
        ], dim=-1)  # shape: (B, L, L, num_heads, 2*d_k + d_edge_head + 2*d_diff_head)

        attention_scores = self.attn_linear(hi_hj_concat).squeeze(-1)  # shape: (B, L, L, num_heads)
        
        # Mask the diagonal (self-attention) by setting it to a large negative value
        mask = torch.eye(L).bool().unsqueeze(0).unsqueeze(-1).to(node_representation.device)  # shape: (1, L, L, 1)
        attention_scores.masked_fill_(mask, float('-inf'))
        
        attention_probs = F.softmax(self.leaky_relu(attention_scores), dim=2)  # shape: (B, L, L, num_heads)

        # Aggregating features correctly along the L dimension
        node_representation_Wh = self.Wh(node_representation)  # shape: (B, L, num_heads, d_k)
        node_representation_Wh = node_representation_Wh.permute(0, 2, 1, 3)  # shape: (B, num_heads, L, d_k)
        
        aggregated_features = torch.matmul(attention_probs.permute(0, 3, 1, 2), node_representation_Wh)  # shape: (B, num_heads, L, d_k)
        aggregated_features = aggregated_features.permute(0, 2, 1, 3)  # shape: (B, L, num_heads, d_k)

        new_node_representation = node_representation + self.leaky_relu(aggregated_features)  # shape: (B, L, num_heads, d_k)

        return new_node_representation


    def single_head_attention_edge(self, node_representation, edge_representation, diff_vec):
        # Update edge representation
        B, L, num_heads, d_k = node_representation.size()
        d_edge_head = edge_representation.size(-1)
        d_diff_head = diff_vec.size(-1)

        hi = node_representation.unsqueeze(2)  # shape: (B, L, 1, num_heads, d_k)
        hj = node_representation.unsqueeze(1)  # shape: (B, 1, L, num_heads, d_k)
        diff_i = diff_vec.unsqueeze(2)  # shape: (B, L, 1, num_heads, d_diff_head)
        diff_j = diff_vec.unsqueeze(1)  # shape: (B, 1, L, num_heads, d_diff_head)

        hi_hj_concat = torch.cat([edge_representation, 
                                  hi.expand(-1, -1, L, -1, -1), 
                                  hj.expand(-1, L, -1, -1, -1),
                                  diff_i.expand(-1, -1, L, -1, -1), 
                                  diff_j.expand(-1, L, -1, -1, -1)], dim=-1)  # shape: (B, L, L, num_heads, 2*d_k + d_edge_head + 2*d_diff_head)

        new_edge_representation = self.edge_linear(hi_hj_concat)  # shape: (B, L, L, num_heads, d_edge_head)

        return new_edge_representation


class CrossGraphAttention(nn.Module):
    def __init__(self, d_node, d_cross_edge, d_diff, num_heads, negative_slope=0.2):
        super(CrossGraphAttention, self).__init__()
        assert d_node % num_heads == 0, "d_node must be divisible by num_heads"
        assert d_cross_edge % num_heads == 0, "d_edge must be divisible by num_heads"
        assert d_diff % num_heads == 0, "d_diff must be divisible by num_heads"

        self.num_heads = num_heads
        self.d_k = d_node // num_heads
        self.d_edge_head = d_cross_edge // num_heads
        self.d_diff_head = d_diff // num_heads

        self.Wn = nn.Linear(d_node, d_node)
        self.Wh = nn.Linear(self.d_k, self.d_k)
        self.We = nn.Linear(d_cross_edge, d_cross_edge)
        self.Wn_2 = nn.Linear(d_node, d_node)
        self.We_2 = nn.Linear(d_cross_edge, d_cross_edge)
        self.Wd = nn.Linear(d_diff, d_diff)
        self.Wd_2 = nn.Linear(d_diff, d_diff)
        self.attn_linear_target = nn.Linear(self.d_k * 2 + self.d_edge_head + self.d_diff_head, 1, bias=False)
        self.attn_linear_binder = nn.Linear(self.d_k * 2 + self.d_edge_head, 1, bias=False)
        self.edge_linear = nn.Linear(self.d_k * 2 + self.d_edge_head + self.d_diff_head, self.d_edge_head)

        self.out_proj_node = nn.Linear(d_node, d_node)
        self.out_proj_edge = nn.Linear(d_cross_edge, d_cross_edge)

        self.leaky_relu = nn.LeakyReLU(negative_slope)

    def forward(self, target_representation, binder_representation, edge_representation, diff_vec):
        B, L1, d_node = target_representation.size()
        L2 = binder_representation.size()[1]
        d_edge = edge_representation.size(-1)

        # pdb.set_trace()
        
        # Multi-head projection
        target_proj = self.Wn(target_representation).view(B, L1, self.num_heads, self.d_k)  
        binder_proj = self.Wn(binder_representation).view(B, L2, self.num_heads, self.d_k)          
        edge_proj = self.We(edge_representation).view(B, L1, L2, self.num_heads, self.d_edge_head)  
        diff_proj = self.Wd(diff_vec).view(B, L1, self.num_heads, self.d_diff_head)   

        # Edge representation update
        new_edge_representation = self.single_head_attention_edge(target_proj, binder_proj, edge_proj, diff_proj)

        concatenated_edge_rep = new_edge_representation.view(B, L1, L2, -1)  
        new_edge_representation = self.out_proj_edge(concatenated_edge_rep)

        # Node representation update
        target_proj_2 = self.Wn_2(target_representation).view(B, L1, self.num_heads, self.d_k)  
        binder_proj_2 = self.Wn_2(binder_representation).view(B, L2, self.num_heads, self.d_k)  
        edge_proj_2 = self.We_2(new_edge_representation).view(B, L1, L2, self.num_heads, self.d_edge_head)  
        diff_proj_2 = self.Wd_2(diff_vec).view(B, L1, self.num_heads, self.d_diff_head)   
        
        new_target_representation = self.single_head_attention_target(target_proj_2, binder_proj_2, edge_proj_2, diff_proj_2)
        new_binder_representation = self.single_head_attention_binder(binder_proj_2, target_proj_2, edge_proj_2)
        
        concatenated_target_rep = new_target_representation.view(B, L1, -1)  
        new_target_representation = self.out_proj_node(concatenated_target_rep)

        concatenated_binder_rep = new_binder_representation.view(B, L2, -1)  
        new_binder_representation = self.out_proj_node(concatenated_binder_rep)

        return new_target_representation, new_binder_representation, new_edge_representation

    def single_head_attention_target(self, target_representation, binder_representation, edge_representation, diff_vec):
        # Update target node representation
        # pdb.set_trace()
        B, L1, num_heads, d_k = target_representation.size()
        L2 = binder_representation.size(1)
        d_edge_head = edge_representation.size(-1)
        d_diff_head = diff_vec.size(-1)

        hi = target_representation.unsqueeze(2)  # shape: (B, L1, 1, num_heads, d_k)
        hj = binder_representation.unsqueeze(1)  # shape: (B, 1, L2, num_heads, d_k)
        diff_i = diff_vec.unsqueeze(2)  # shape: (B, L1, 1, num_heads, d_diff_head)

        # Concatenate hi, hj, edge_representation, and diff_i
        hi_hj_concat = torch.cat([
            hi.expand(-1, -1, L2, -1, -1), 
            hj.expand(-1, L1, -1, -1, -1), 
            edge_representation, 
            diff_i.expand(-1, -1, L2, -1, -1)
        ], dim=-1)  # shape: (B, L1, L2, num_heads, 2*d_k + d_edge_head + d_diff_head)

        # Calculate attention scores
        attention_scores = self.attn_linear_target(hi_hj_concat).squeeze(-1)  # shape: (B, L1, L2, num_heads)
        attention_probs = F.softmax(self.leaky_relu(attention_scores), dim=2)  # shape: (B, L1, L2, num_heads)

        # Aggregating features correctly along the L2 dimension
        binder_representation_Wh = self.Wh(binder_representation)  # shape: (B, L2, num_heads, d_k)
        binder_representation_Wh = binder_representation_Wh.permute(0, 2, 1, 3)  # shape: (B, num_heads, L2, d_k)
        
        aggregated_features = torch.matmul(attention_probs.permute(0, 3, 1, 2), binder_representation_Wh)  # shape: (B, num_heads, L1, d_k)
        aggregated_features = aggregated_features.permute(0, 2, 1, 3)  # shape: (B, L1, num_heads, d_k)

        # Update target representation
        new_target_representation = target_representation + self.leaky_relu(aggregated_features)  # shape: (B, L1, num_heads, d_k)

        return new_target_representation


    def single_head_attention_binder(self, target_representation, binder_representation, edge_representation):
        # Update target node representation
        # pdb.set_trace()
        B, L1, num_heads, d_k = target_representation.size()
        L2 = binder_representation.size(1)
        d_edge_head = edge_representation.size(-1)

        hi = target_representation.unsqueeze(2)  # shape: (B, L1, 1, num_heads, d_k)
        hj = binder_representation.unsqueeze(1)  # shape: (B, 1, L2, num_heads, d_k)
        edge_representation = edge_representation.transpose(1,2)

        # Concatenate hi, hj, edge_representation, and diff_i
        hi_hj_concat = torch.cat([
            hi.expand(-1, -1, L2, -1, -1), 
            hj.expand(-1, L1, -1, -1, -1), 
            edge_representation, 
        ], dim=-1)  # shape: (B, L1, L2, num_heads, 2*d_k + d_edge_head)

        # Calculate attention scores
        attention_scores = self.attn_linear_binder(hi_hj_concat).squeeze(-1)  # shape: (B, L1, L2, num_heads)
        attention_probs = F.softmax(self.leaky_relu(attention_scores), dim=2)  # shape: (B, L1, L2, num_heads)

        # Aggregating features correctly along the L2 dimension
        binder_representation_Wh = self.Wh(binder_representation)  # shape: (B, L2, num_heads, d_k)
        binder_representation_Wh = binder_representation_Wh.permute(0, 2, 1, 3)  # shape: (B, num_heads, L2, d_k)
        
        aggregated_features = torch.matmul(attention_probs.permute(0, 3, 1, 2), binder_representation_Wh)  # shape: (B, num_heads, L1, d_k)
        aggregated_features = aggregated_features.permute(0, 2, 1, 3)  # shape: (B, L1, num_heads, d_k)

        # Update target representation
        new_target_representation = target_representation + self.leaky_relu(aggregated_features)  # shape: (B, L1, num_heads, d_k)

        return new_target_representation


    def single_head_attention_edge(self, target_representation, binder_representation, edge_representation, diff_vec):
        # Update edge representation
        # pdb.set_trace()
        B, L1, num_heads, d_k = target_representation.size()
        L2 = binder_representation.size(1)
        d_edge_head = edge_representation.size(-1)
        d_diff_head = diff_vec.size(-1)

        hi = target_representation.unsqueeze(2)  # shape: (B, L1, 1, num_heads, d_k)
        hj = binder_representation.unsqueeze(1)  # shape: (B, 1, L2, num_heads, d_k)
        diff_i = diff_vec.unsqueeze(2)  # shape: (B, L1, 1, num_heads, d_diff_head)

        hi_hj_concat = torch.cat([edge_representation, 
                                  hi.expand(-1, -1, L2, -1, -1), 
                                  hj.expand(-1, L1, -1, -1, -1), 
                                  diff_i.expand(-1, -1, L2, -1, -1)], dim=-1)  # shape: (B, L1, L2, num_heads, 2*d_k + d_edge_head + d_diff_head)

        new_edge_representation = self.edge_linear(hi_hj_concat)  # shape: (B, L1, L2, num_heads, d_edge_head)

        return new_edge_representation