Commit 5c165b81 authored by rusty1s's avatar rusty1s
Browse files

cuda test fixes

parent b69d826f
import torch
from .edgewise_spline_weighting_cpu import EdgewiseSplineWeightingCPU
if torch.cuda.is_available():
from .edgewise_spline_weighting_gpu import EdgewiseSplineWeightingGPU
......@@ -10,4 +8,4 @@ def edgewise_spline_weighting(input, weight, amount, index):
if input.is_cuda:
return EdgewiseSplineWeightingGPU(amount, index)(input, weight)
else:
return EdgewiseSplineWeightingCPU(amount, index)(input, weight)
raise NotImplementedError
import torch
from torch.autograd import Function
class EdgewiseSplineWeightingCPU(Function):
def __init__(self, amount, index):
super(EdgewiseSplineWeightingCPU, self).__init__()
self.amount = amount
self.index = index
def forward(self, input, weight):
self.save_for_backward(input, weight)
_, M_in, M_out = weight.size()
k_max = self.amount.size(1)
output = input.new(input.size(0), M_out).fill_(0)
for k in range(k_max):
b = self.amount[:, k] # [|E|]
c = self.index[:, k] # [|E|]
for i in range(M_in):
w = weight[:, i] # [K x M_out]
w = w[c] # [|E| x M_out]
f = input[:, i] # [|E|]
# Need to transpose twice, so we can make use of broadcasting.
output += (f * b * w.t()).t() # [|E| x M_out]
return output
def backward(self, grad_output):
input, weight = self.saved_tensors
K, M_in, M_out = weight.size()
k_max = self.amount.size(1)
num_edges = input.size(0)
grad_input = grad_output.new(num_edges, M_in).fill_(0)
grad_weight = grad_output.new(K, M_in, M_out).fill_(0)
for k in range(k_max):
b = self.amount[:, k] # [|E|]
c = self.index[:, k] # [|E|]
c_expand = c.contiguous().view(-1, 1).expand(c.size(0), M_out)
for i in range(M_in):
w = weight[:, i] # [K x M_out]
w = w[c] # [|E| x M_out]
f = b * torch.sum(grad_output * w, dim=1) # [|E|]
grad_input[:, i] += f
f = input[:, i] # [|E|]
w_grad = (f * b * grad_output.t()).t() # [|E|, M_out]
grad_weight[:, i, :].scatter_add_(0, c_expand, w_grad)
return grad_input, grad_weight
......@@ -14,16 +14,14 @@ class EdgewiseSplineWeightingGPUTest(unittest.TestCase):
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_forward(self):
input = [[0.25, 0.125], [0.25, 0.375], [0.75, 0.625], [0.75, 0.875]]
input = torch.FloatTensor(input)
kernel_size = torch.LongTensor([3, 4])
is_open_spline = torch.LongTensor([1, 0])
input = torch.cuda.FloatTensor(input)
kernel_size = torch.cuda.LongTensor([3, 4])
is_open_spline = torch.cuda.LongTensor([1, 0])
amount, index = spline(input, kernel_size, is_open_spline, 12, 1)
amount, index = amount.cuda(), index.cuda()
input = torch.FloatTensor([[1, 2], [3, 4], [5, 6], [7, 8]])
weight = torch.arange(0.5, 0.5 * 25, step=0.5).view(12, 2, 1)
input, weight = input.cuda(), weight.cuda()
input = torch.cuda.FloatTensor([[1, 2], [3, 4], [5, 6], [7, 8]])
weight = torch.arange(0.5, 0.5 * 25, step=0.5).view(12, 2, 1).cuda()
input, weight = Variable(input), Variable(weight)
op = EdgewiseSplineWeightingGPU(amount, index)
......@@ -41,16 +39,14 @@ class EdgewiseSplineWeightingGPUTest(unittest.TestCase):
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_backward(self):
input = [[0.25, 0.125], [0.25, 0.375], [0.75, 0.625], [0.75, 0.875]]
input = torch.DoubleTensor(input)
kernel_size = torch.LongTensor([3, 4])
is_open_spline = torch.LongTensor([1, 0])
input = torch.cuda.DoubleTensor(input)
kernel_size = torch.cuda.LongTensor([3, 4])
is_open_spline = torch.cuda.LongTensor([1, 0])
amount, index = spline(input, kernel_size, is_open_spline, 12, 1)
amount, index = amount.cuda(), index.cuda()
input = torch.randn(4, 2).double()
weight = torch.randn(12, 2, 1).double()
input, weight = input.cuda(), weight.cuda()
input = torch.randn(4, 2).double().cuda()
weight = torch.randn(12, 2, 1).double().cuda()
input = Variable(input, requires_grad=True)
weight = Variable(weight, requires_grad=True)
......
import torch
from .spline_cpu import spline_cpu
if torch.cuda.is_available():
from .spline_linear_gpu import spline_linear_gpu
from .spline_quadratic_gpu import spline_quadratic_gpu
......@@ -19,4 +17,4 @@ def spline(input, kernel_size, is_open_spline, K, degree):
else:
raise NotImplementedError()
else:
return spline_cpu(input, kernel_size, is_open_spline, degree)
raise NotImplementedError()
......@@ -32,8 +32,7 @@ def spline_conv(
r = row.view(-1, 1).expand(row.size(0), output.size(1))
output = zero.scatter_add_(0, Variable(r), output)
# Weighten root node features by multiplying with the meaned weights from
# the origin.
# Weighten root node features by multiplying with root weight.
output += torch.mm(input, weight[-1])
# Normalize output by degree.
......
from __future__ import division
from unittest import TestCase
import unittest
import torch
from torch.autograd import Variable
from numpy.testing import assert_almost_equal
......@@ -8,38 +8,9 @@ from numpy.testing import assert_almost_equal
from .spline_conv import spline_conv
class SplineGcnTest(TestCase):
def test_forward_cpu(self):
edges = torch.LongTensor([[0, 0, 0, 0], [1, 2, 3, 4]])
values = [[0.25, 0.125], [0.25, 0.375], [0.75, 0.625], [0.75, 0.875]]
values = torch.FloatTensor(values)
adj = torch.sparse.FloatTensor(edges, values, torch.Size([5, 5, 2]))
kernel_size = torch.LongTensor([3, 4])
is_open_spline = torch.LongTensor([1, 0])
input = torch.FloatTensor([[9, 10], [1, 2], [3, 4], [5, 6], [7, 8]])
weight = torch.arange(0.5, 0.5 * 27, step=0.5).view(13, 2, 1)
input, weight = Variable(input), Variable(weight)
output = spline_conv(
adj, input, weight, kernel_size, is_open_spline, K=12, degree=1)
expected_output = [
[(12.5 * 9 + 13 * 10 + 266) / 4],
[12.5 * 1 + 13 * 2],
[12.5 * 3 + 13 * 4],
[12.5 * 5 + 13 * 6],
[12.5 * 7 + 13 * 8],
]
assert_almost_equal(output.cpu().data.numpy(), expected_output, 1)
class SplineConvTest(unittest.TestCase):
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_forward_gpu(self):
if not torch.cuda.is_available():
return
edges = torch.LongTensor([[0, 0, 0, 0], [1, 2, 3, 4]])
values = [[0.25, 0.125], [0.25, 0.375], [0.75, 0.625], [0.75, 0.875]]
values = torch.FloatTensor(values)
......@@ -66,9 +37,3 @@ class SplineGcnTest(TestCase):
]
assert_almost_equal(output.cpu().data.numpy(), expected_output, 1)
def test_backward_cpu(self):
pass
def test_backward_gpu(self):
pass
from functools import reduce
from itertools import product
import torch
def _spline_cpu(input, kernel_size, is_open_spline, degree):
"""
Args:
input (Tensor): 1d or 2d tensor.
kernel_size (list)
is_open_spline (list)
spline_degree (int, optional): B-Spline degree. (default: 1)
"""
if degree != 1:
raise NotImplementedError()
input = input.unsqueeze(1) if len(input.size()) < 2 else input
input = input * (kernel_size - is_open_spline).type_as(input)
amount = input.frac()
amount = torch.stack([amount, 1 - amount], dim=len(input.size()))
bot = input.floor().long()
top = (bot + 1) % kernel_size
bot %= kernel_size
index = torch.stack([top, bot], dim=len(input.size()))
return amount, index
def _create_mask(dim, m, type=torch.LongTensor):
mask = list(product(*[range(m) for _ in range(dim)]))
mask = torch.LongTensor(mask).type(type)
mask += torch.arange(0, dim * m, m).type_as(mask)
return mask
def spline_cpu(input, kernel_size, is_open_spline, degree):
amount, index = _spline_cpu(input, kernel_size, is_open_spline, degree)
dim = amount.size(1)
m = amount.size(2)
mask = _create_mask(dim, m, index.type())
amount = amount.view(-1, m * dim)
amount = amount[:, mask.view(-1)]
amount = amount.view(-1, m**dim, dim)
amount = amount.prod(2)
off = [reduce(lambda x, y: x * y, kernel_size[i:]) for i in range(1, dim)]
off.append(1)
off = torch.LongTensor([off]).type_as(index).t()
index = off * index
index = index.view(-1, m * dim)
index = index[:, mask.view(-1)]
index = index.view(-1, m**dim, dim)
index = index.sum(2)
return amount, index
from unittest import TestCase
import torch
from numpy.testing import assert_equal, assert_almost_equal
from .spline_cpu import spline_cpu
class SplineCPUTest(TestCase):
def test_open_spline(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.LongTensor([5])
is_open_spline = torch.LongTensor([1])
amount, index = spline_cpu(input, kernel_size, is_open_spline, 1)
a = [[0, 1], [0.2, 0.8], [0, 1], [0, 1], [0, 1], [0.8, 0.2], [0, 1]]
i = [[1, 0], [1, 0], [2, 1], [3, 2], [4, 3], [4, 3], [0, 4]]
assert_almost_equal(amount.numpy(), a, 1)
assert_equal(index.numpy(), i)
def test_closed_spline(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.LongTensor([4])
is_open_spline = torch.LongTensor([0])
amount, index = spline_cpu(input, kernel_size, is_open_spline, 1)
a = [[0, 1], [0.2, 0.8], [0, 1], [0, 1], [0, 1], [0.8, 0.2], [0, 1]]
i = [[1, 0], [1, 0], [2, 1], [3, 2], [0, 3], [0, 3], [1, 0]]
assert_almost_equal(amount.numpy(), a, 1)
assert_equal(index.numpy(), i)
def test_spline_2d(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
input = torch.stack([input, input], dim=1)
kernel_size = torch.LongTensor([5, 4])
is_open_spline = torch.LongTensor([1, 0])
amount, index = spline_cpu(input, kernel_size, is_open_spline, 1)
expected_amount = [
[0, 0, 0, 1],
[0.04, 0.16, 0.16, 0.64],
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1],
[0.64, 0.16, 0.16, 0.04],
[0, 0, 0, 1],
]
expected_index = [
[5, 4, 1, 0],
[5, 4, 1, 0],
[10, 9, 6, 5],
[15, 14, 11, 10],
[16, 19, 12, 15],
[16, 19, 12, 15],
[1, 0, 17, 16],
]
assert_almost_equal(amount.numpy(), expected_amount, 2)
assert_equal(index.numpy(), expected_index)
def test_spline_3d(self):
input = torch.FloatTensor([0.05, 0.25, 0.5, 0.75, 0.95, 1])
input = torch.stack([input, input, input], dim=1)
kernel_size = torch.LongTensor([5, 4, 4])
is_open_spline = torch.LongTensor([1, 0, 0])
amount, index = spline_cpu(input, kernel_size, is_open_spline, 1)
self.assertEqual(list(amount.size()), [6, 8])
self.assertEqual(list(index.size()), [6, 8])
self.assertLess(index.max(), 5 * 4 * 4)
......@@ -10,12 +10,11 @@ if torch.cuda.is_available():
class SplineQuadraticGPUTest(unittest.TestCase):
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_open_spline(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.LongTensor([7])
is_open_spline = torch.LongTensor([1])
input = torch.cuda.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.cuda.LongTensor([7])
is_open_spline = torch.cuda.LongTensor([1])
a1, i1 = spline_cubic_gpu(input.cuda(),
kernel_size.cuda(), is_open_spline.cuda(), 7)
a1, i1 = spline_cubic_gpu(input, kernel_size, is_open_spline, 7)
a2 = [
[0.1667, 0.6667, 0.1667, 0],
......@@ -34,12 +33,11 @@ class SplineQuadraticGPUTest(unittest.TestCase):
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_closed_spline(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.LongTensor([4])
is_open_spline = torch.LongTensor([0])
input = torch.cuda.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.cuda.LongTensor([4])
is_open_spline = torch.cuda.LongTensor([0])
a1, i1 = spline_cubic_gpu(input.cuda(),
kernel_size.cuda(), is_open_spline.cuda(), 4)
a1, i1 = spline_cubic_gpu(input, kernel_size, is_open_spline, 4)
a2 = [
[0.1667, 0.6667, 0.1667, 0],
......
import unittest
import torch
from numpy.testing import assert_equal
from .spline_cpu import spline_cpu
from numpy.testing import assert_equal, assert_almost_equal
if torch.cuda.is_available():
from .spline_linear_gpu import spline_linear_gpu
......@@ -12,60 +10,28 @@ if torch.cuda.is_available():
class SplineLinearGPUTest(unittest.TestCase):
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_open_spline(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.LongTensor([5])
is_open_spline = torch.LongTensor([1])
input = torch.cuda.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.cuda.LongTensor([5])
is_open_spline = torch.cuda.LongTensor([1])
a1, i1 = spline_cpu(input, kernel_size, is_open_spline, 1)
a1, i1 = spline_linear_gpu(input, kernel_size, is_open_spline, 5)
a2, i2 = spline_linear_gpu(input.cuda(),
kernel_size.cuda(), is_open_spline.cuda(),
5)
a2 = [[0, 1], [0.2, 0.8], [0, 1], [0, 1], [0, 1], [0.8, 0.2], [0, 1]]
i2 = [[1, 0], [1, 0], [2, 1], [3, 2], [4, 3], [4, 3], [0, 4]]
assert_equal(a1.numpy(), a2.cpu().numpy())
assert_equal(i1.numpy(), i2.cpu().numpy())
assert_almost_equal(a1.cpu().numpy(), a2, 2)
assert_equal(i1.cpu().numpy(), i2)
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_closed_spline(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.LongTensor([4])
is_open_spline = torch.LongTensor([0])
a1, i1 = spline_cpu(input, kernel_size, is_open_spline, 1)
a2, i2 = spline_linear_gpu(input.cuda(),
kernel_size.cuda(), is_open_spline.cuda(),
4)
input = torch.cuda.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.cuda.LongTensor([4])
is_open_spline = torch.cuda.LongTensor([0])
assert_equal(a1.numpy(), a2.cpu().numpy())
assert_equal(i1.numpy(), i2.cpu().numpy())
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_spline_2d(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
input = torch.stack([input, input], dim=1)
kernel_size = torch.LongTensor([5, 4])
is_open_spline = torch.LongTensor([1, 0])
a1, i1 = spline_cpu(input, kernel_size, is_open_spline, 1)
a2, i2 = spline_linear_gpu(input.cuda(),
kernel_size.cuda(),
is_open_spline.cuda(), 20)
assert_equal(a1.numpy(), a2.cpu().numpy())
# assert_equal(i1.numpy(), i2.cpu().numpy())
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_spline_3d(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
input = torch.stack([input, input, input], dim=1)
kernel_size = torch.LongTensor([5, 4, 4])
is_open_spline = torch.LongTensor([1, 0, 0])
a1, i1 = spline_cpu(input, kernel_size, is_open_spline, 1)
a1, i1 = spline_linear_gpu(input, kernel_size, is_open_spline, 4)
a2, i2 = spline_linear_gpu(input.cuda(),
kernel_size.cuda(),
is_open_spline.cuda(), 80)
a2 = [[0, 1], [0.2, 0.8], [0, 1], [0, 1], [0, 1], [0.8, 0.2], [0, 1]]
i2 = [[1, 0], [1, 0], [2, 1], [3, 2], [0, 3], [0, 3], [1, 0]]
# assert_equal(a1.numpy(), a2.cpu().numpy())
# assert_equal(i1.numpy(), i2.cpu().numpy())
assert_almost_equal(a1.cpu().numpy(), a2, 2)
assert_equal(i1.cpu().numpy(), i2)
......@@ -10,13 +10,11 @@ if torch.cuda.is_available():
class SplineQuadraticGPUTest(unittest.TestCase):
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_open_spline(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.LongTensor([6])
is_open_spline = torch.LongTensor([1])
input = torch.cuda.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.cuda.LongTensor([6])
is_open_spline = torch.cuda.LongTensor([1])
a1, i1 = spline_quadratic_gpu(input.cuda(),
kernel_size.cuda(),
is_open_spline.cuda(), 6)
a1, i1 = spline_quadratic_gpu(input, kernel_size, is_open_spline, 6)
a2 = [[0.5, 0.5, 0], [0.32, 0.66, 0.02], [0.5, 0.5, 0], [0.5, 0.5, 0],
[0.5, 0.5, 0], [0.02, 0.66, 0.32], [0.5, 0.5, 0]]
......@@ -28,13 +26,11 @@ class SplineQuadraticGPUTest(unittest.TestCase):
@unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
def test_closed_spline(self):
input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.LongTensor([4])
is_open_spline = torch.LongTensor([0])
input = torch.cuda.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
kernel_size = torch.cuda.LongTensor([4])
is_open_spline = torch.cuda.LongTensor([0])
a1, i1 = spline_quadratic_gpu(input.cuda(),
kernel_size.cuda(),
is_open_spline.cuda(), 4)
a1, i1 = spline_quadratic_gpu(input, kernel_size, is_open_spline, 4)
a2 = [[0.5, 0.5, 0], [0.32, 0.66, 0.02], [0.5, 0.5, 0], [0.5, 0.5, 0],
[0.5, 0.5, 0], [0.02, 0.66, 0.32], [0.5, 0.5, 0]]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment