test_unary_op_sp.py 694 Bytes
Newer Older
Mufei Li's avatar
Mufei Li committed
1
2
3
import sys

import backend as F
4
5
import pytest
import torch
Mufei Li's avatar
Mufei Li committed
6

7
from dgl.sparse import create_from_coo
Mufei Li's avatar
Mufei Li committed
8

Mufei Li's avatar
Mufei Li committed
9
# TODO(#4818): Skipping tests on win.
Mufei Li's avatar
Mufei Li committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
if not sys.platform.startswith("linux"):
    pytest.skip("skipping tests on win", allow_module_level=True)


def test_neg():
    ctx = F.ctx()
    row = torch.tensor([1, 1, 3]).to(ctx)
    col = torch.tensor([1, 2, 3]).to(ctx)
    val = torch.tensor([1.0, 1.0, 2.0]).to(ctx)
    A = create_from_coo(row, col, val)
    neg_A = -A
    assert A.shape == neg_A.shape
    assert A.nnz == neg_A.nnz
    assert torch.allclose(-A.val, neg_A.val)
    assert torch.allclose(torch.stack(A.coo()), torch.stack(neg_A.coo()))
    assert A.val.device == neg_A.val.device