liuganghuggingface
commited on
Upload graph_decoder/layers.py with huggingface_hub
Browse files- graph_decoder/layers.py +132 -0
graph_decoder/layers.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2024 the Llamole team.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
from torch.jit import Final
|
16 |
+
import torch.nn.functional as F
|
17 |
+
from itertools import repeat
|
18 |
+
import collections.abc
|
19 |
+
|
20 |
+
import torch
|
21 |
+
import torch.nn as nn
|
22 |
+
|
23 |
+
class Attention(nn.Module):
|
24 |
+
fast_attn: Final[bool]
|
25 |
+
|
26 |
+
def __init__(
|
27 |
+
self,
|
28 |
+
dim,
|
29 |
+
num_heads=8,
|
30 |
+
qkv_bias=False,
|
31 |
+
qk_norm=False,
|
32 |
+
attn_drop=0,
|
33 |
+
proj_drop=0,
|
34 |
+
norm_layer=nn.LayerNorm,
|
35 |
+
):
|
36 |
+
super().__init__()
|
37 |
+
assert dim % num_heads == 0, "dim should be divisible by num_heads"
|
38 |
+
self.num_heads = num_heads
|
39 |
+
self.head_dim = dim // num_heads
|
40 |
+
|
41 |
+
self.scale = self.head_dim**-0.5
|
42 |
+
self.fast_attn = hasattr(
|
43 |
+
torch.nn.functional, "scaled_dot_product_attention"
|
44 |
+
) # FIXME
|
45 |
+
assert self.fast_attn, "scaled_dot_product_attention Not implemented"
|
46 |
+
|
47 |
+
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
|
48 |
+
|
49 |
+
self.q_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
|
50 |
+
self.k_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
|
51 |
+
self.attn_drop = nn.Dropout(attn_drop)
|
52 |
+
|
53 |
+
self.proj = nn.Linear(dim, dim)
|
54 |
+
self.proj_drop = nn.Dropout(proj_drop)
|
55 |
+
|
56 |
+
def forward(self, x, node_mask):
|
57 |
+
B, N, D = x.shape
|
58 |
+
|
59 |
+
# B, head, N, head_dim
|
60 |
+
qkv = (
|
61 |
+
self.qkv(x)
|
62 |
+
.reshape(B, N, 3, self.num_heads, self.head_dim)
|
63 |
+
.permute(2, 0, 3, 1, 4)
|
64 |
+
)
|
65 |
+
q, k, v = qkv.unbind(0) # B, head, N, head_dim
|
66 |
+
q, k = self.q_norm(q), self.k_norm(k)
|
67 |
+
|
68 |
+
attn_mask = (node_mask[:, None, :, None] & node_mask[:, None, None, :]).expand(
|
69 |
+
-1, self.num_heads, N, N
|
70 |
+
)
|
71 |
+
extended_nodes = (attn_mask.sum(dim=-1) == 0)
|
72 |
+
attn_mask = attn_mask.clone()
|
73 |
+
attn_mask[extended_nodes] = True
|
74 |
+
|
75 |
+
x = F.scaled_dot_product_attention(
|
76 |
+
q,
|
77 |
+
k,
|
78 |
+
v,
|
79 |
+
dropout_p=self.attn_drop.p,
|
80 |
+
attn_mask=attn_mask,
|
81 |
+
)
|
82 |
+
|
83 |
+
x = x.transpose(1, 2).reshape(B, N, -1)
|
84 |
+
# if no extended nodes, set the output to 0
|
85 |
+
# x[~node_mask] = 0
|
86 |
+
|
87 |
+
x = self.proj(x)
|
88 |
+
x = self.proj_drop(x)
|
89 |
+
|
90 |
+
return x
|
91 |
+
|
92 |
+
|
93 |
+
class MLP(nn.Module):
|
94 |
+
def __init__(
|
95 |
+
self,
|
96 |
+
in_features,
|
97 |
+
hidden_features=None,
|
98 |
+
out_features=None,
|
99 |
+
act_layer=nn.GELU,
|
100 |
+
bias=True,
|
101 |
+
drop=0.0,
|
102 |
+
):
|
103 |
+
super().__init__()
|
104 |
+
out_features = out_features or in_features
|
105 |
+
hidden_features = hidden_features or in_features
|
106 |
+
bias = to_2tuple(bias)
|
107 |
+
linear_layer = nn.Linear
|
108 |
+
|
109 |
+
self.fc1 = linear_layer(in_features, hidden_features, bias=bias[0])
|
110 |
+
self.act = act_layer()
|
111 |
+
self.drop1 = nn.Dropout(drop)
|
112 |
+
self.fc2 = linear_layer(hidden_features, out_features, bias=bias[1])
|
113 |
+
|
114 |
+
def forward(self, x):
|
115 |
+
x = self.fc1(x)
|
116 |
+
x = self.act(x)
|
117 |
+
x = self.drop1(x)
|
118 |
+
x = self.fc2(x)
|
119 |
+
return x
|
120 |
+
|
121 |
+
|
122 |
+
# From PyTorch internals
|
123 |
+
def _ntuple(n):
|
124 |
+
def parse(x):
|
125 |
+
if isinstance(x, collections.abc.Iterable) and not isinstance(x, str):
|
126 |
+
return tuple(x)
|
127 |
+
return tuple(repeat(x, n))
|
128 |
+
|
129 |
+
return parse
|
130 |
+
|
131 |
+
|
132 |
+
to_2tuple = _ntuple(2)
|