Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [Unreleased]
### Added
- Added `triton` dependency ([#133](https://github.com/pyg-team/pyg-lib/pull/133))
- Enable `pytest` testing ([#132](https://github.com/pyg-team/pyg-lib/pull/132))
- Added C++-based autograd and TorchScript support for `segment_matmul` ([#120](https://github.com/pyg-team/pyg-lib/pull/120), [#122](https://github.com/pyg-team/pyg-lib/pull/122))
- Allow overriding `time` for seed nodes via `seed_time` in `neighbor_sample` ([#118](https://github.com/pyg-team/pyg-lib/pull/118))
Expand Down
8 changes: 8 additions & 0 deletions pyg_lib/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def wrapper(*args, **kwargs):
return wrapper


def onlyCUDA(func: Callable) -> Callable:
import pytest
return pytest.mark.skipif(
not torch.cuda.is_available(),
reason="CUDA not available",
)(func)


def withCUDA(func: Callable) -> Callable:
def wrapper(*args, **kwargs):
func(*args, device=torch.device('cpu'), **kwargs)
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ test=pytest
[tool:pytest]
addopts=--capture=no

[flake8]
ignore=E731

[isort]
multi_line_output=3
include_trailing_comma=True
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def build_extension(self, ext):
cwd=self.build_temp)


install_requires = []
install_requires = [
'triton',
]

test_requires = [
'pytest',
Expand Down
37 changes: 37 additions & 0 deletions test/test_triton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import torch
import triton
import triton.language as tl
from torch import Tensor

from pyg_lib.testing import onlyCUDA


@triton.jit
def add_kernel(x_ptr, y_ptr, out_ptr, numel, **meta):
pid = tl.program_id(axis=0)
block_start = pid * meta['BLOCK_SIZE']

offsets = block_start + tl.arange(0, meta['BLOCK_SIZE'])
mask = offsets < numel

x = tl.load(x_ptr + offsets, mask=mask)
y = tl.load(y_ptr + offsets, mask=mask)

output = x + y

tl.store(out_ptr + offsets, output, mask=mask)


def add(x: Tensor, y: Tensor) -> Tensor:
out = torch.empty_like(x)
assert x.is_cuda and y.is_cuda and out.is_cuda
grid = lambda meta: (triton.cdiv(x.numel(), meta['BLOCK_SIZE']), )
add_kernel[grid](x, y, out, x.numel(), BLOCK_SIZE=1024)
return out


@onlyCUDA
def test_triton():
x = torch.rand(100, device='cuda')
y = torch.rand(100, device='cuda')
assert torch.allclose(x + y, add(x, y))