forked from hxttkl/GraphAdapter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraphadapter.py
212 lines (185 loc) · 8.63 KB
/
graphadapter.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
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
import torch
import torch.nn.functional as F
import torch.nn as nn
import torch_sparse
import numpy as np
from typing import Union, Tuple, Optional
import torch_geometric as tg
from transformers import activations
from torch_geometric.nn.conv import MessagePassing
from torch_geometric.nn import GATConv,GATv2Conv,SuperGATConv,ResGatedGraphConv,GCN2Conv,GatedGraphConv
from torch_geometric.nn import GCNConv
from torch_geometric.nn import SAGEConv
from torch_geometric.nn import GINConv
from torch_geometric.nn import APPNP
from torch.nn import Linear
from torch_geometric.nn.inits import glorot, zeros
from torch_geometric.typing import (
Adj,
OptTensor,
PairTensor,
SparseTensor,
torch_sparse,
Tensor
)
from torch_geometric.utils import (
add_self_loops,
is_torch_sparse_tensor,
remove_self_loops,
softmax,
)
class LlamaRMSNorm(nn.Module):
def __init__(self, hidden_size, eps=1e-6):
"""
LlamaRMSNorm is equivalent to T5LayerNorm
"""
super().__init__()
self.weight = nn.Parameter(torch.ones(hidden_size))
self.variance_epsilon = eps
def forward(self, hidden_states):
input_dtype = hidden_states.dtype
hidden_states = hidden_states.to(torch.float32)
variance = hidden_states.pow(2).mean(-1, keepdim=True)
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
return self.weight * hidden_states.to(input_dtype)
class MLPBlock(MessagePassing):
def __init__(self,in_shape, hiddensize=64, num_layers=2, batch_norm=True, **kwargs):
super().__init__(aggr='mean',**kwargs)
self.num_layers=num_layers
self.lin1 = torch.nn.Linear(in_shape,hiddensize*2,bias=False)
self.lin2 = torch.nn.Linear(hiddensize*2,hiddensize,bias=False)
self.bn_first1 = LlamaRMSNorm(hiddensize)
self.ACT2FN = activations.ACT2FN['silu']
self.GNN1 = torch.nn.Linear(hiddensize,hiddensize,bias=False)
self.is_gnn = is_gnn
def forward(self, x,edge_index):
x = (self.lin1(x))
x = self.ACT2FN(x)
x = self.bn_first1(self.lin2(x))
x = self.ACT2FN(self.GNN1(x))
return x
def message(self, x_j,index: Tensor, ptr: OptTensor, size_i: Optional[int]) -> Tensor:
return x_j
class SAGEBlock(MessagePassing):
def __init__(self,in_shape, hiddensize=64, num_layers=2, batch_norm=True, is_gnn = True, **kwargs):
super().__init__(aggr='mean',**kwargs)
self.num_layers=num_layers
self.lin1 = torch.nn.Linear(in_shape,hiddensize*2,bias=False)
self.lin2 = torch.nn.Linear(hiddensize*2,hiddensize,bias=False)
self.bn_first1 = LlamaRMSNorm(hiddensize)
self.ACT2FN = activations.ACT2FN['silu']
self.GNN1 = torch.nn.Linear(hiddensize,hiddensize,bias=False)
self.is_gnn = is_gnn
def forward(self, x,edge_index):
x = (self.lin1(x))
x = self.ACT2FN(x)
x = self.bn_first1(self.lin2(x))
if(self.is_gnn==True):
x = self.propagate(edge_index, x=x,size=None)
x = self.ACT2FN(self.GNN1(x))
if(self.is_gnn==True):
x = self.propagate(edge_index, x=x,size=None)
return x
def message(self, x_j,index: Tensor, ptr: OptTensor, size_i: Optional[int]) -> Tensor:
return x_j
class GATBlock(MessagePassing):
def __init__(self,in_shape, hiddensize=64, num_layers=2, **kwargs):
super().__init__(aggr='sum',**kwargs)
self.num_layers=num_layers
self.lin1 = torch.nn.Linear(in_shape,hiddensize*2,bias=False)
self.lin2 = torch.nn.Linear(hiddensize*2,hiddensize,bias=False)
self.bn_first1 = LlamaRMSNorm(hiddensize)
self.bn_first2 = LlamaRMSNorm(hiddensize)
self.bn_first3 = LlamaRMSNorm(hiddensize)
self.ACT2FN = activations.ACT2FN['silu']
self.GNN1 = torch.nn.Linear(hiddensize,hiddensize,bias=False)
self.GNN2 = torch.nn.Linear(hiddensize,hiddensize,bias=False)
self.heads = 8
self.out_channels = hiddensize//self.heads
self.is_gnn = is_gnn
#self.att = torch.nn.Parameter(torch.randn(1, self.heads, 2 * self.out_channels))
#self.att1 = torch.nn.Parameter(torch.randn(1, self.heads, 2 * self.out_channels))
self.att_l = torch.nn.Linear(hiddensize,hiddensize)
self.att_r = torch.nn.Linear(hiddensize,hiddensize)
self.att_l1 = torch.nn.Linear(hiddensize,hiddensize)
self.att_r1 = torch.nn.Linear(hiddensize,hiddensize)
self.sqrt=1/np.sqrt(self.out_channels)
def forward(self, x,edge_index):
x = (self.lin1(x))
x = self.ACT2FN(x)
x = self.bn_first1(self.lin2(x))
if(self.is_gnn==True):
x = self.propagate(edge_index, x=x,size=None,layer=0)
x = self.bn_first2(self.ACT2FN(self.GNN1(x)))
if(self.is_gnn==True):
x = self.propagate(edge_index, x=x,size=None,layer=1)
x = self.bn_first3(self.GNN2(x))
return x
def message(self,x_i, x_j,index: Tensor, ptr: OptTensor, size_i: Optional[int],layer) -> Tensor:
if(layer==0):
x_i = self.att_l(x_i)
x_j = self.att_r(x_j)
else:
x_i = self.att_l1(x_i)
x_j = self.att_r1(x_j)
x_j = x_j.view(-1, self.heads, self.out_channels)
x_i = x_i.view(-1, self.heads, self.out_channels)
alpha = (x_i*x_j).sum(dim=-1)*self.sqrt
alpha = softmax(alpha, index, ptr, size_i)
return (x_j * alpha.view(-1, self.heads, 1)).view(-1, self.heads * self.out_channels)
class FusionBlock(MessagePassing):
def __init__(self, gnn_size, llm_size, hidden_size,is_pretraining, **kwargs):
super().__init__(aggr='mean',**kwargs)
self.hidden_size = hidden_size
self.llm_size = llm_size
self.gnn_size = gnn_size
self.prompt_lin = torch.nn.Linear(llm_size,hidden_size,bias=False)
self.g_lin = torch.nn.Linear(hidden_size,hidden_size,bias=False)
self.fuse1 = torch.nn.Linear(hidden_size*2,hidden_size*10,bias=False)
self.fuse2 = torch.nn.Linear(hidden_size*10,hidden_size,bias=False)
self.extend = torch.nn.Linear(hidden_size,llm_size,bias=False)
self.ACT2FN = activations.ACT2FN['silu']
self.is_pretraining = is_pretraining
def forward(self, x,node_ids, prompt):
node_ids = node_ids.view(-1)
token = self.prompt_lin(prompt)
out = x[node_ids]
out = self.g_lin(out)
out = torch.cat((out,token),dim=1)
out = self.ACT2FN(self.fuse1(out))
out = self.fuse2(out)
if(self.is_pretraining):
out = self.extend(out)
return out
def message(self, x_j, k: OptTensor,v,
index: Tensor, ptr: OptTensor,q,
size_i: Optional[int]) -> Tensor:
v = v
return v
class GraphAdapter(torch.nn.Module):
def __init__(self,llm_shape, hiddensize_gnn=64, hiddensize_fusion = 64, num_layers=2, GNN_type='SAGE', is_pretraining=True):
super(GraphAdapter,self).__init__()
if(GNN_type == 'SAGE'):
self.graph_encode = SAGEBlock(llm_shape, hiddensize = hiddensize_gnn, num_layers=num_layers)
elif(GNN_type == 'GAT'):
self.graph_encode = GATBlock(llm_shape, hiddensize = hiddensize_gnn, num_layers=num_layers)
elif(GNN_type == 'MLP'):
self.graph_encode = MLPBlock(llm_shape, hiddensize = hiddensize_gnn, num_layers=num_layers)
else:
raise "GNN_type should be SAGE, GAT, MLP"
self.fuse_model = FusionBlock(hiddensize_gnn, llm_shape, hiddensize_fusion,is_pretraining)
def forward(self, x,edge_index,node_ids=None,prompt=None):
gx = self.graph_encode(x,edge_index)
out = self.fuse_model(gx,node_ids,prompt)
return out
##used for downstream task
class LinearHead(torch.nn.Module):
def __init__(self, x_shape, y_shape, pretrain_args):
super(LinearHead,self).__init__()
self.ga = GraphAdapter(llm_shape = x_shape, hiddensize_gnn = pretrain_args.hiddensize_gnn, hiddensize_fusion = pretrain_args.hiddensize_fusion, GNN_type=pretrain_args.GNN_type, num_layers=pretrain_args.num_layers,is_pretraining=False)
self.lin = torch.nn.Linear(pretrain_args.hiddensize_fusion,y_shape)
self.lin.weight = torch.nn.Parameter((self.lin.weight-self.lin.weight.mean()/self.lin.weight.std())*0.1210,requires_grad=True) ## since
def forward(self, x,edge_index,prompt_embedding):
x = self.ga(x,edge_index,torch.arange(len(x)),prompt_embedding)
x = self.lin(x)
return F.log_softmax(x,dim=1),x