Commit 1f019c20 authored by rusty1s's avatar rusty1s
Browse files

fill value added

parent 10542ab2
[ [
{ {
"name": "add", "name": "add",
"output": [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]],
"index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]], "index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
"input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]], "input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
"dim": 1, "dim": 1,
"fill_value": 0,
"grad": [[10, 20, 30, 40, 50, 60], [15, 25, 35, 45, 55, 65]], "grad": [[10, 20, 30, 40, 50, 60], [15, 25, 35, 45, 55, 65]],
"expected": [[50, 60, 50, 30, 40], [15, 15, 35, 35, 25]] "expected": [[50, 60, 50, 30, 40], [15, 15, 35, 35, 25]]
}, },
{ {
"name": "max", "name": "max",
"output": [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]],
"index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]], "index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
"input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]], "input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
"dim": 1, "dim": 1,
"fill_value": 0,
"grad": [[10, 20, 30, 40, 50, 60], [15, 25, 35, 45, 55, 65]], "grad": [[10, 20, 30, 40, 50, 60], [15, 25, 35, 45, 55, 65]],
"expected": [[50, 60, 0, 30, 40], [0, 15, 0, 35, 25]] "expected": [[50, 60, 0, 30, 40], [0, 15, 0, 35, 25]]
} }
......
[ [
{ {
"name": "add", "name": "add",
"output": [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]],
"index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]], "index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
"input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]], "input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
"dim": 1, "dim": 1,
"fill_value": 0,
"expected": [[0, 0, 4, 3, 3, 0], [2, 4, 4, 0, 0, 0]] "expected": [[0, 0, 4, 3, 3, 0], [2, 4, 4, 0, 0, 0]]
}, },
{ {
"name": "sub", "name": "sub",
"output": [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]],
"index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]], "index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
"input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]], "input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
"dim": 1, "dim": 1,
"fill_value": 0,
"expected": [[0, 0, -4, -3, -3, 0], [-2, -4, -4, 0, 0, 0]] "expected": [[0, 0, -4, -3, -3, 0], [-2, -4, -4, 0, 0, 0]]
}, },
{ {
"name": "mul", "name": "mul",
"output": [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]],
"index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]], "index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
"input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]], "input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
"dim": 1, "dim": 1,
"fill_value": 1,
"expected": [[1, 1, 4, 3, 2, 0], [0, 4, 3, 1, 1, 1]] "expected": [[1, 1, 4, 3, 2, 0], [0, 4, 3, 1, 1, 1]]
},
{
"name": "div",
"index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
"input": [[2, 1, 1, 4, 2], [1, 2, 1, 2, 4]],
"dim": 1,
"fill_value": 1,
"expected": [[1, 1, 0.25, 0.5, 0.5, 1], [0.5, 0.25, 0.5, 1, 1, 1]]
},
{
"name": "mean",
"index": [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
"input": [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
"dim": 1,
"fill_value": 0,
"expected": [[0, 0, 4, 3, 1.5, 0], [1, 4, 2, 0, 0, 0]]
} }
] ]
...@@ -17,11 +17,12 @@ f.close() ...@@ -17,11 +17,12 @@ f.close()
@pytest.mark.parametrize('tensor,i', product(tensors, range(len(data)))) @pytest.mark.parametrize('tensor,i', product(tensors, range(len(data))))
def test_backward_cpu(tensor, i): def test_backward_cpu(tensor, i):
name = data[i]['name'] name = data[i]['name']
output = V(Tensor(tensor, data[i]['output']))
index = V(torch.LongTensor(data[i]['index'])) index = V(torch.LongTensor(data[i]['index']))
input = V(Tensor(tensor, data[i]['input']), requires_grad=True) input = V(Tensor(tensor, data[i]['input']), requires_grad=True)
dim = data[i]['dim'] dim = data[i]['dim']
fill_value = data[i]['fill_value']
grad = Tensor(tensor, data[i]['grad']) grad = Tensor(tensor, data[i]['grad'])
output = V(grad.new(grad.size()).fill_(fill_value))
expected = Tensor(tensor, data[i]['expected']) expected = Tensor(tensor, data[i]['expected'])
func = getattr(torch_scatter, 'scatter_{}_'.format(name)) func = getattr(torch_scatter, 'scatter_{}_'.format(name))
...@@ -34,11 +35,12 @@ def test_backward_cpu(tensor, i): ...@@ -34,11 +35,12 @@ def test_backward_cpu(tensor, i):
@pytest.mark.parametrize('tensor,i', product(tensors, range(len(data)))) @pytest.mark.parametrize('tensor,i', product(tensors, range(len(data))))
def test_backward_gpu(tensor, i): def test_backward_gpu(tensor, i):
name = data[i]['name'] name = data[i]['name']
output = V(Tensor(tensor, data[i]['output'])).cuda()
index = V(torch.LongTensor(data[i]['index'])).cuda() index = V(torch.LongTensor(data[i]['index'])).cuda()
input = V(Tensor(tensor, data[i]['input']), requires_grad=True).cuad() input = V(Tensor(tensor, data[i]['input']), requires_grad=True).cuda()
dim = data[i]['dim'] dim = data[i]['dim']
fill_value = data[i]['fill_value']
grad = Tensor(tensor, data[i]['grad']).cuda() grad = Tensor(tensor, data[i]['grad']).cuda()
output = V(grad.new(grad.size()).fill_(fill_value)).cuda()
expected = Tensor(tensor, data[i]['expected']) expected = Tensor(tensor, data[i]['expected'])
func = getattr(torch_scatter, 'scatter_{}_'.format(name)) func = getattr(torch_scatter, 'scatter_{}_'.format(name))
......
...@@ -16,18 +16,19 @@ f.close() ...@@ -16,18 +16,19 @@ f.close()
@pytest.mark.parametrize('tensor,i', product(tensors, range(len(data)))) @pytest.mark.parametrize('tensor,i', product(tensors, range(len(data))))
def test_forward_cpu(tensor, i): def test_forward_cpu(tensor, i):
name = data[i]['name'] name = data[i]['name']
output = Tensor(tensor, data[i]['output'])
index = torch.LongTensor(data[i]['index']) index = torch.LongTensor(data[i]['index'])
input = Tensor(tensor, data[i]['input']) input = Tensor(tensor, data[i]['input'])
dim = data[i]['dim'] dim = data[i]['dim']
expected = Tensor(tensor, data[i]['expected']) fill_value = data[i]['fill_value']
expected = torch.FloatTensor(data[i]['expected']).type_as(input)
output = expected.new(expected.size()).fill_(fill_value)
func = getattr(torch_scatter, 'scatter_{}_'.format(name)) func = getattr(torch_scatter, 'scatter_{}_'.format(name))
func(output, index, input, dim) func(output, index, input, dim)
assert output.tolist() == expected.tolist() assert output.tolist() == expected.tolist()
func = getattr(torch_scatter, 'scatter_{}'.format(name)) func = getattr(torch_scatter, 'scatter_{}'.format(name))
output = func(index, input, dim) output = func(index, input, dim, fill_value=fill_value)
assert output.tolist() == expected.tolist() assert output.tolist() == expected.tolist()
...@@ -35,16 +36,17 @@ def test_forward_cpu(tensor, i): ...@@ -35,16 +36,17 @@ def test_forward_cpu(tensor, i):
@pytest.mark.parametrize('tensor,i', product(tensors, range(len(data)))) @pytest.mark.parametrize('tensor,i', product(tensors, range(len(data))))
def test_forward_gpu(tensor, i): def test_forward_gpu(tensor, i):
name = data[i]['name'] name = data[i]['name']
output = Tensor(tensor, data[i]['output']).cuda()
index = torch.LongTensor(data[i]['index']).cuda() index = torch.LongTensor(data[i]['index']).cuda()
input = Tensor(tensor, data[i]['input']).cuda() input = Tensor(tensor, data[i]['input']).cuda()
dim = data[i]['dim'] dim = data[i]['dim']
expected = Tensor(tensor, data[i]['expected']) fill_value = data[i]['fill_value']
expected = torch.FloatTensor(data[i]['expected']).type_as(input)
output = expected.new(expected.size()).fill_(fill_value).cuda()
func = getattr(torch_scatter, 'scatter_{}_'.format(name)) func = getattr(torch_scatter, 'scatter_{}_'.format(name))
func(output, index, input, dim) func(output, index, input, dim)
assert output.cpu().tolist() == expected.tolist() assert output.cpu().tolist() == expected.tolist()
func = getattr(torch_scatter, 'scatter_{}'.format(name)) func = getattr(torch_scatter, 'scatter_{}'.format(name))
output = func(index, input, dim) output = func(index, input, dim, fill_value=fill_value)
assert output.cpu().tolist() == expected.tolist() assert output.cpu().tolist() == expected.tolist()
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