forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: - Add example triton test to exercise triton integration - Add NCCL integration Pull Request resolved: pytorch#2624 Reviewed By: sryap, jianyuh Differential Revision: D57741140 Pulled By: q10 fbshipit-source-id: 3529139b907893d92231902b3ae34d4914142b6b
- Loading branch information
1 parent
2ae4309
commit 0ac664b
Showing
9 changed files
with
164 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <nccl.h> | ||
|
||
namespace fbgemm_gpu::experimental { | ||
|
||
void example_nccl_code() { | ||
ncclComm_t comms[4]; | ||
int devs[4] = {0, 1, 2, 3}; | ||
ncclCommInitAll(comms, 4, devs); | ||
|
||
for (int i = 0; i < 4; i++) { | ||
ncclCommDestroy(comms[i]); | ||
} | ||
} | ||
|
||
} // namespace fbgemm_gpu::experimental |
74 changes: 74 additions & 0 deletions
74
fbgemm_gpu/experimental/example/test/triton_example_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import unittest | ||
|
||
import torch | ||
import triton | ||
import triton.language as tl | ||
|
||
|
||
@triton.jit | ||
# fmt: off | ||
def triton_add_kernel(x_ptr, y_ptr, z_ptr, n_elements, BLOCK_SIZE: tl.constexpr) -> None: | ||
# fmt: on | ||
# We use a 1D launch grid so axis is 0. | ||
pid = tl.program_id(axis=0) | ||
|
||
# Compute the offsets in BLOCK_SIZE chunks. | ||
offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) | ||
|
||
# Create a mask to guard memory operations against out-of-bounds accesses. | ||
mask = offsets < n_elements | ||
|
||
# Load x and y from DRAM. | ||
x = tl.load(x_ptr + offsets, mask=mask) | ||
y = tl.load(y_ptr + offsets, mask=mask) | ||
|
||
# Sum and write back to DRAM. | ||
output = x + y | ||
tl.store(z_ptr + offsets, output, mask=mask) | ||
|
||
|
||
def triton_add(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: | ||
# Pre-allocate the output. | ||
output = torch.empty_like(x) | ||
assert x.is_cuda and y.is_cuda and output.is_cuda | ||
|
||
# Create the SPMD launch grid. It can be either Tuple[int], or | ||
# Callable(metaparameters) -> Tuple[int]. In this case, we use a 1D grid | ||
# where the size is the number of blocks: | ||
n_elements = output.numel() | ||
grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),) # noqa: E731 | ||
|
||
# Launch the kernel. | ||
# | ||
# Each torch.tensor object is implicitly converted into a pointer to its | ||
# first element. | ||
# | ||
# `triton.jit`'ed functions can be indexed with a launch grid to obtain a | ||
# callable GPU kernel. | ||
# | ||
# Pass meta-parameters as keywords arguments. | ||
# pyre-ignore | ||
triton_add_kernel[grid](x, y, output, n_elements, BLOCK_SIZE=1024) | ||
|
||
# We return a handle to z but, since `torch.cuda.synchronize()` hasn't been | ||
# called, the kernel is still running asynchronously at this point. | ||
return output | ||
|
||
|
||
@unittest.skipIf( | ||
not torch.cuda.is_available(), | ||
"Requires CUDA to run", | ||
) | ||
class TestTriton(unittest.TestCase): | ||
def test_triton_example(self) -> None: | ||
size = 98432 | ||
X = torch.rand(size, device="cuda") | ||
Y = torch.rand(size, device="cuda") | ||
|
||
torch.testing.assert_close(triton_add(X, Y).cpu(), (X + Y).cpu()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters