File size: 8,049 Bytes
89650c1 |
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 |
import os
import sys
import torch.backends.openmp
from torch import autograd
from torch.utils import cpp_extension
module = sys.modules[__name__]
class RSPMMAddMulFunction(autograd.Function):
@staticmethod
def forward(ctx, edge_index, edge_type, edge_weight, relation, input):
node_in, node_out = edge_index
key = node_in * (node_out.max() + 1) + node_out
assert (key.diff() >= 0).all(), "Expect sorted `edge_index`"
if input.device.type == "cuda":
forward = rspmm.rspmm_add_mul_forward_cuda
else:
forward = rspmm.rspmm_add_mul_forward_cpu
output = forward(edge_index, edge_type, edge_weight, relation, input)
ctx.save_for_backward(edge_index, edge_type, edge_weight, relation, input, output)
return output
@staticmethod
def backward(ctx, output_grad):
if output_grad.device.type == "cuda":
backward = rspmm.rspmm_add_mul_backward_cuda
else:
backward = rspmm.rspmm_add_mul_backward_cpu
weight_grad, relation_grad, input_grad = backward(*ctx.saved_tensors, output_grad)
return None, None, weight_grad, relation_grad, input_grad
class RSPMMMinMulFunction(autograd.Function):
@staticmethod
def forward(ctx, edge_index, edge_type, edge_weight, relation, input):
node_in, node_out = edge_index
key = node_in * (node_out.max() + 1) + node_out
assert (key.diff() >= 0).all(), "Expect sorted `edge_index`"
if input.device.type == "cuda":
forward = rspmm.rspmm_min_mul_forward_cuda
else:
forward = rspmm.rspmm_min_mul_forward_cpu
output = forward(edge_index, edge_type, edge_weight, relation, input)
ctx.save_for_backward(edge_index, edge_type, edge_weight, relation, input, output)
return output
@staticmethod
def backward(ctx, output_grad):
if output_grad.device.type == "cuda":
backward = rspmm.rspmm_min_mul_backward_cuda
else:
backward = rspmm.rspmm_min_mul_backward_cpu
weight_grad, relation_grad, input_grad = backward(*ctx.saved_tensors, output_grad)
return None, None, weight_grad, relation_grad, input_grad
class RSPMMMaxMulFunction(autograd.Function):
@staticmethod
def forward(ctx, edge_index, edge_type, edge_weight, relation, input):
node_in, node_out = edge_index
key = node_in * (node_out.max() + 1) + node_out
assert (key.diff() >= 0).all(), "Expect sorted `edge_index`"
if input.device.type == "cuda":
forward = rspmm.rspmm_max_mul_forward_cuda
else:
forward = rspmm.rspmm_max_mul_forward_cpu
output = forward(edge_index, edge_type, edge_weight, relation, input)
ctx.save_for_backward(edge_index, edge_type, edge_weight, relation, input, output)
return output
@staticmethod
def backward(ctx, output_grad):
if output_grad.device.type == "cuda":
backward = rspmm.rspmm_max_mul_backward_cuda
else:
backward = rspmm.rspmm_max_mul_backward_cpu
weight_grad, relation_grad, input_grad = backward(*ctx.saved_tensors, output_grad)
return None, None, weight_grad, relation_grad, input_grad
class RSPMMAddAddFunction(autograd.Function):
@staticmethod
def forward(ctx, edge_index, edge_type, edge_weight, relation, input):
node_in, node_out = edge_index
key = node_in * (node_out.max() + 1) + node_out
assert (key.diff() >= 0).all(), "Expect sorted `edge_index`"
if input.device.type == "cuda":
forward = rspmm.rspmm_add_add_forward_cuda
else:
forward = rspmm.rspmm_add_add_forward_cpu
output = forward(edge_index, edge_type, edge_weight, relation, input)
ctx.save_for_backward(edge_index, edge_type, edge_weight, relation, input, output)
return output
@staticmethod
def backward(ctx, output_grad):
if output_grad.device.type == "cuda":
backward = rspmm.rspmm_add_add_backward_cuda
else:
backward = rspmm.rspmm_add_add_backward_cpu
weight_grad, relation_grad, input_grad = backward(*ctx.saved_tensors, output_grad)
return None, None, weight_grad, relation_grad, input_grad
class RSPMMMinAddFunction(autograd.Function):
@staticmethod
def forward(ctx, edge_index, edge_type, edge_weight, relation, input):
node_in, node_out = edge_index
key = node_in * (node_out.max() + 1) + node_out
assert (key.diff() >= 0).all(), "Expect sorted `edge_index`"
if input.device.type == "cuda":
forward = rspmm.rspmm_min_add_forward_cuda
else:
forward = rspmm.rspmm_min_add_forward_cpu
output = forward(edge_index, edge_type, edge_weight, relation, input)
ctx.save_for_backward(edge_index, edge_type, edge_weight, relation, input, output)
return output
@staticmethod
def backward(ctx, output_grad):
if output_grad.device.type == "cuda":
backward = rspmm.rspmm_min_add_backward_cuda
else:
backward = rspmm.rspmm_min_add_backward_cpu
weight_grad, relation_grad, input_grad = backward(*ctx.saved_tensors, output_grad)
return None, None, weight_grad, relation_grad, input_grad
class RSPMMMaxAddFunction(autograd.Function):
@staticmethod
def forward(ctx, edge_index, edge_type, edge_weight, relation, input):
node_in, node_out = edge_index
key = node_in * (node_out.max() + 1) + node_out
assert (key.diff() >= 0).all(), "Expect sorted `edge_index`"
if input.device.type == "cuda":
forward = rspmm.rspmm_max_add_forward_cuda
else:
forward = rspmm.rspmm_max_add_forward_cpu
output = forward(edge_index, edge_type, edge_weight, relation, input)
ctx.save_for_backward(edge_index, edge_type, edge_weight, relation, input, output)
return output
@staticmethod
def backward(ctx, output_grad):
if output_grad.device.type == "cuda":
backward = rspmm.rspmm_max_add_backward_cuda
else:
backward = rspmm.rspmm_max_add_backward_cpu
weight_grad, relation_grad, input_grad = backward(*ctx.saved_tensors, output_grad)
return None, None, weight_grad, relation_grad, input_grad
def generalized_rspmm(edge_index, edge_type, edge_weight, relation, input, sum="add", mul="mul"):
name = "RSPMM%s%sFunction" % (sum.capitalize(), mul.capitalize())
if not hasattr(module, name):
raise ValueError("No generalized rspmm implementation found for summation `%s` and multiplication `%s`"
% (sum, mul))
Function = getattr(module, name)
node_in, node_out = edge_index
key = node_in * (node_out.max() + 1) + node_out
order = key.argsort()
return Function.apply(edge_index[:, order], edge_type[order], edge_weight[order], relation, input)
def load_extension(name, sources, extra_cflags=None, extra_cuda_cflags=None, **kwargs):
if extra_cflags is None:
extra_cflags = ["-Ofast"]
if torch.backends.openmp.is_available():
extra_cflags += ["-fopenmp", "-DAT_PARALLEL_OPENMP"]
else:
extra_cflags.append("-DAT_PARALLEL_NATIVE")
if extra_cuda_cflags is None:
if torch.cuda.is_available():
extra_cuda_cflags = ["-O3"]
extra_cflags.append("-DCUDA_OP")
else:
new_sources = []
for source in sources:
if not cpp_extension._is_cuda_file(source):
new_sources.append(source)
sources = new_sources
return cpp_extension.load(name, sources, extra_cflags, extra_cuda_cflags, **kwargs)
print("Load rspmm extension. This may take a while...")
path = os.path.join(os.path.dirname(__file__), "source")
rspmm = load_extension("rspmm", [os.path.join(path, "rspmm.cpp"), os.path.join(path, "rspmm.cu")]) |