-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollator_graphormer.py
74 lines (63 loc) · 1.97 KB
/
collator_graphormer.py
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import torch
def pad_1d_unsqueeze(x, padlen):
x = x + 1 # pad id = 0
xlen = x.size(0)
if xlen < padlen:
new_x = x.new_zeros([padlen], dtype=x.dtype)
new_x[:xlen] = x
x = new_x
return x.unsqueeze(0)
def pad_2d_unsqueeze(x, padlen):
x = x + 1 # pad id = 0
xlen, xdim = x.size()
if xlen < padlen:
new_x = x.new_zeros([padlen, xdim], dtype=x.dtype)
new_x[:xlen, :] = x
x = new_x
return x.unsqueeze(0)
def pad_attn_bias_unsqueeze(x, padlen):
xlen = x.size(0)
if xlen < padlen:
new_x = x.new_zeros(
[padlen, padlen], dtype=x.dtype).fill_(float('-inf'))
new_x[:xlen, :xlen] = x
new_x[xlen:, :xlen] = 0
x = new_x
return x.unsqueeze(0)
def pad_edge_type_unsqueeze(x, padlen):
xlen = x.size(0)
if xlen < padlen:
new_x = x.new_zeros([padlen, padlen, x.size(-1)], dtype=x.dtype)
new_x[:xlen, :xlen, :] = x
x = new_x
return x.unsqueeze(0)
def pad_spatial_pos_unsqueeze(x, padlen):
x = x + 1
xlen = x.size(0)
if xlen < padlen:
new_x = x.new_zeros([padlen, padlen], dtype=x.dtype)
new_x[:xlen, :xlen] = x
x = new_x
return x.unsqueeze(0)
def pad_3d_unsqueeze(x, padlen1, padlen2, padlen3):
x = x + 1
xlen1, xlen2, xlen3, xlen4 = x.size()
if xlen1 < padlen1 or xlen2 < padlen2 or xlen3 < padlen3:
new_x = x.new_zeros([padlen1, padlen2, padlen3, xlen4], dtype=x.dtype)
new_x[:xlen1, :xlen2, :xlen3, :] = x
x = new_x
return x.unsqueeze(0)
class Batch():
def __init__(self, **kwargs):
self.attrs = kwargs.keys()
for k, v in kwargs.items():
setattr(self, k, v)
def to(self, device):
for k in self.attrs:
v = getattr(self, k)
setattr(self, k, v.to(device))
return self
def __len__(self):
return self.in_degree.size(0)