-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
1 parent
1f01a18
commit 897cb2a
Showing
17 changed files
with
275 additions
and
135 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
from cacheflow import activation_ops | ||
|
||
|
||
class SiluAndMul(nn.Module): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
def forward( | ||
self, | ||
x: torch.Tensor, # (num_tokens, 2 * d) | ||
) -> torch.Tensor: # (num_tokens, d) | ||
num_tokens = x.shape[0] | ||
d = x.shape[1] // 2 | ||
out = torch.empty(num_tokens, d, dtype=x.dtype, device=x.device) | ||
activation_ops.silu_and_mul(out, x) | ||
return out |
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,12 @@ | ||
#include <torch/extension.h> | ||
|
||
void silu_and_mul( | ||
torch::Tensor& out, | ||
torch::Tensor& input); | ||
|
||
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { | ||
m.def( | ||
"silu_and_mul", | ||
&silu_and_mul, | ||
"Activation function used in SwiGLU."); | ||
} |
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,46 @@ | ||
#include <torch/extension.h> | ||
#include <ATen/cuda/CUDAContext.h> | ||
|
||
namespace cacheflow { | ||
|
||
template<typename T> | ||
__device__ __forceinline__ T silu(const T& x) { | ||
// x * sigmoid(x) | ||
return (T) (((float) x) / (1.0f + expf((float) -x))); | ||
} | ||
|
||
template<typename scalar_t> | ||
__global__ void silu_and_mul_kernel( | ||
scalar_t* __restrict__ out, // [num_tokens, d] | ||
const scalar_t* __restrict__ input, // [num_tokens, 2, d] | ||
const int d) { | ||
const int token_idx = blockIdx.x; | ||
for (int idx = threadIdx.x; idx < d; idx += blockDim.x) { | ||
const scalar_t x = __ldg(&input[token_idx * 2 * d + idx]); | ||
const scalar_t y = __ldg(&input[token_idx * 2 * d + d + idx]); | ||
out[token_idx * d + idx] = silu(x) * y; | ||
} | ||
} | ||
|
||
} // namespace cacheflow | ||
|
||
void silu_and_mul( | ||
torch::Tensor& out, // [num_tokens, d] | ||
torch::Tensor& input) // [num_tokens, 2 * d] | ||
{ | ||
int num_tokens = input.size(0); | ||
int d = input.size(1) / 2; | ||
|
||
dim3 grid(num_tokens); | ||
dim3 block(std::min(d, 1024)); | ||
const cudaStream_t stream = at::cuda::getCurrentCUDAStream(); | ||
AT_DISPATCH_FLOATING_TYPES_AND_HALF( | ||
input.scalar_type(), | ||
"silu_and_mul_kernel", | ||
[&] { | ||
cacheflow::silu_and_mul_kernel<scalar_t><<<grid, block, 0, stream>>>( | ||
out.data_ptr<scalar_t>(), | ||
input.data_ptr<scalar_t>(), | ||
d); | ||
}); | ||
} |
Oops, something went wrong.