Skip to content

Commit

Permalink
ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
InnocentBug committed Nov 6, 2024
1 parent fcab4fd commit 45fdd98
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
6 changes: 5 additions & 1 deletion python/pytest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import pytest


@pytest.fixture(scope="session")
def ptens_cuda_support():
import ptens_base

string = ptens_base.status_str().split("\n")
for line in string:
if "CUDA support" in line:
Expand All @@ -14,18 +16,20 @@ def ptens_cuda_support():
return False
assert False


@pytest.fixture(scope="session")
def device(ptens_cuda_support):

device = os.environ["TORCH_TEST_DEVICE"]

if "cuda" in device:
assert ptens_cuda_support
import torch

assert torch.cuda.is_available()

return device


@pytest.fixture(scope="session")
def float_epsilon():
return 1e-5
28 changes: 15 additions & 13 deletions python/pytest/test_doc.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import torch
import ptens


def test_create_tensor(device):
A = torch.ones((3,5))
A = torch.ones((3, 5))
sum_a = float(torch.sum(A**2))
assert A.shape == (3,5)
assert A.shape == (3, 5)
B = A.to(device)
sum_b = float(torch.sum(B**2))
assert device in str(B.device)
assert abs(sum_a - sum_b) < 1e-6


def test_create_ptensor0(device, float_epsilon):
A = ptens.ptensor0.randn([2],5)
A = ptens.ptensor0.randn([2], 5)
sum_a = float(torch.sum(A**2))
B = A.to(device)
sum_b = float(torch.sum(B**2))
Expand All @@ -23,37 +24,38 @@ def test_create_ptensor0(device, float_epsilon):
assert sum_a > 1e-3
assert abs(sum_a - sum_b) < float_epsilon


def test_create_ptensor1(device, float_epsilon):
A = ptens.ptensor1.randn([1,2,3],5)
A = ptens.ptensor1.randn([1, 2, 3], 5)
sum_a = float(torch.sum(A**2))
B = A.to(device)
sum_b = float(torch.sum(B**2))
print(B)
assert B.atoms == [1,2,3]
assert B.shape == (3,5)
assert B.atoms == [1, 2, 3]
assert B.shape == (3, 5)
assert device in str(B.device)
assert abs(sum_a - sum_b) < float_epsilon
assert sum_a > 1e-3


def test_create_ptensor2(device, float_epsilon):
A=ptens.ptensor2.randn([1,2,3],5)
A = ptens.ptensor2.randn([1, 2, 3], 5)
sum_a = float(torch.sum(A**2))
B = A.to(device)
sum_b = float(torch.sum(B**2))
print(B)
assert B.atoms == [1,2,3]
assert B.shape == (3,3,5)
assert B.atoms == [1, 2, 3]
assert B.shape == (3, 3, 5)
assert device in str(B.device)
assert abs(sum_a - sum_b) < float_epsilon
assert sum_a > 1e-3


def test_sequential(device, float_epsilon):
A = ptens.ptensor1.sequential([1,2,3],5)
A = ptens.ptensor1.sequential([1, 2, 3], 5)
sum_a = float(torch.sum(A))
B = A.to(device)
sum_b = float(torch.sum(B))

assert abs(sum_a - sum_b) < float_epsilon
assert abs(sum_a - sum(range(3*5))) < float_epsilon

assert abs(sum_a - sum(range(3 * 5))) < float_epsilon
5 changes: 2 additions & 3 deletions python/pytest/test_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
from ptens_base import atomspack
from torch.autograd.gradcheck import gradcheck, gradgradcheck


def test_bug1(device):
nnodes = 15
graph =ptens.ggraph.random(nnodes,0.5)
graph = ptens.ggraph.random(nnodes, 0.5)
print(graph)
subgraphs = [ptens.subgraph.trivial(), ptens.subgraph.edge()]
node_values = torch.rand(nnodes, 1, requires_grad=True)

node_attributes = ptens.subgraphlayer0.from_matrix(graph, ptens.subgraph.trivial(), node_values)


for sg in subgraphs:
gather_features = ptens.subgraphlayer0.gather(sg, node_attributes)
result = torch.sum(gather_features)
Expand All @@ -26,4 +26,3 @@ def test_bug1(device):

check = gradcheck(ptens.subgraphlayer0.gather, (sg, node_attributes), eps=1e-3)
print(check)

37 changes: 18 additions & 19 deletions python/pytest/test_tests4.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@
import ptens_base as pb
import ptens as p


def test_ptensorlayer(device, float_epsilon):
atoms = pb.atomspack.from_list([[1, 3, 4], [2, 5], [0, 2]])
atoms2 = pb.atomspack.random(5, 5, 0.6)

atoms=pb.atomspack.from_list([[1,3,4],[2,5],[0,2]])
atoms2=pb.atomspack.random(5,5,0.6)
A0 = p.ptensorlayer0.randn(atoms, 3)
A1 = p.ptensorlayer1.randn(atoms, 3)
A2 = p.ptensorlayer2.randn(atoms, 3)

A0=p.ptensorlayer0.randn(atoms,3)
A1=p.ptensorlayer1.randn(atoms,3)
A2=p.ptensorlayer2.randn(atoms,3)
B0 = p.ptensorlayer1.gather(atoms2, A0)
B1 = p.ptensorlayer1.gather(atoms2, A1)
B2 = p.ptensorlayer1.gather(atoms2, A2)

B0=p.ptensorlayer1.gather(atoms2,A0)
B1=p.ptensorlayer1.gather(atoms2,A1)
B2=p.ptensorlayer1.gather(atoms2,A2)

A0g=A0.to(device)
A1g=A1.to(device)
A2g=A2.to(device)
A0g = A0.to(device)
A1g = A1.to(device)
A2g = A2.to(device)

B0g=p.ptensorlayer1.gather(atoms2,A0g)
B1g=p.ptensorlayer1.gather(atoms2,A1g)
B2g=p.ptensorlayer1.gather(atoms2,A2g)
B0g = p.ptensorlayer1.gather(atoms2, A0g)
B1g = p.ptensorlayer1.gather(atoms2, A1g)
B2g = p.ptensorlayer1.gather(atoms2, A2g)

# Making sure we don't just compute on zeros
assert torch.norm(B0) > float_epsilon
assert torch.norm(B1) > float_epsilon
assert torch.norm(B2) > float_epsilon

assert torch.allclose(B0, B0g.to("cpu"))
assert torch.allclose(B1g.to('cpu'), B1)
assert torch.allclose(B2g.to('cpu'), B2)

assert torch.allclose(B0, B0g.to("cpu"))
assert torch.allclose(B1g.to("cpu"), B1)
assert torch.allclose(B2g.to("cpu"), B2)

0 comments on commit 45fdd98

Please sign in to comment.