-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
25 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
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 |
71 changes: 71 additions & 0 deletions
71
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,71 @@ | ||
# 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. | ||
|
||
# pyre-strict | ||
|
||
import unittest | ||
|
||
import torch | ||
import triton | ||
import triton.language as tl | ||
|
||
|
||
@triton.jit | ||
def softmax_triton(Y, stride_ym, stride_yn, X, stride_xm, stride_xn, M, N): | ||
# Row index | ||
m = tl.program_id(0) | ||
|
||
# Column indices. This specific kernel only works for matrices that have | ||
# less than BLOCK_SIZE columns | ||
BLOCK_SIZE = 1024 | ||
n = tl.arange(0, BLOCK_SIZE) | ||
|
||
# Compute the memory address of all the elements that we want to load | ||
X = X + m * stride_xm + n * stride_xn | ||
|
||
# Load input data; pad out-of-bounds elements with 0 | ||
x = tl.load(X, mask=n < N, other=-float('inf')) | ||
|
||
# Compute numerically-stable softmax | ||
z = x - tl.max(x, axis=0) | ||
num = tl.exp(z) | ||
denom = tl.sum(num, axis=0) | ||
y = num / denom | ||
|
||
# write back to Y | ||
Y = Y + m * stride_ym + n * stride_yn | ||
tl.store(Y, y, mask=n < N) | ||
|
||
|
||
@torch.jit.script | ||
def softmax_torch(x): | ||
x_max = x.max(dim=1)[0] | ||
numerator = torch.exp(x) | ||
denominator = numerator.sum(dim=1) | ||
return numerator / denominator[:, None] | ||
|
||
|
||
@unittest.skipIf( | ||
not torch.cuda.is_available(), | ||
"Requires CUDA to run", | ||
) | ||
class TestTriton(unittest.TestCase): | ||
def test_triton_example(self) -> None: | ||
# Allocate input/output tensors | ||
X = torch.normal(0, 1, size=(583, 931), device='cuda') | ||
Y = torch.empty_like(X) | ||
|
||
# SPMD launch grid | ||
grid = (X.shape[0], ) | ||
|
||
# Enqueue GPU kernel | ||
softmax_triton[grid]( | ||
Y, Y.stride(0), Y.stride(1), | ||
X, X.stride(0), X.stride(1), | ||
X.shape[0] , X.shape[1] | ||
) | ||
|
||
torch.testing.assert_close(Y.cpu(), softmax_torch(X).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