-
Notifications
You must be signed in to change notification settings - Fork 2
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
94eea2d
commit f545648
Showing
4 changed files
with
59 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .cuda_kernel import VectorAddition_CUDA, vector_addition_cuda | ||
from .naive import VectorAddition_Naive, vector_addition_naive | ||
from .pytorch import VectorAddition_PyTorch, vector_addition_pytorch | ||
from .triton_kernel import VectorAddition_Triton, vector_addition_triton |
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,37 @@ | ||
from typing import Tuple | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
def _vector_addition_naive( | ||
x: torch.Tensor, y: torch.Tensor, output: torch.Tensor, num_elements: int, BLOCK_SIZE: int | ||
) -> None: | ||
for block_start in range(0, num_elements, BLOCK_SIZE): | ||
block_end = max(block_start + BLOCK_SIZE, num_elements) | ||
|
||
output[block_start:block_end] = x[block_start:block_end] + y[block_start:block_end] | ||
|
||
|
||
class _VectorAddition_Naive(torch.autograd.Function): | ||
def forward(ctx, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: | ||
assert x.dim() == 1 | ||
output = torch.empty_like(x) | ||
|
||
num_elements = x.numel() | ||
|
||
_vector_addition_naive(x, y, output, num_elements, BLOCK_SIZE=1024) | ||
|
||
return output | ||
|
||
def backward(ctx, output_grad: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: | ||
return output_grad, output_grad | ||
|
||
|
||
def vector_addition_naive(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: | ||
return _VectorAddition_Naive.apply(x, y) | ||
|
||
|
||
class VectorAddition_Naive(nn.Module): | ||
def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: | ||
return vector_addition_naive(x, y) |
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