forked from InternLM/InternEvo
-
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.
- Loading branch information
Showing
153 changed files
with
12,938 additions
and
6,448 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
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,37 @@ | ||
#include <torch/extension.h> | ||
#include <c10/cuda/CUDAGuard.h> | ||
|
||
#define CHECK_DEVICE(x) TORCH_CHECK(x.device().type() == torch::kCUDA, #x " must be on CUDA") | ||
#define CHECK_SHAPE(x, ...) TORCH_CHECK(x.sizes() == torch::IntArrayRef({__VA_ARGS__}), #x " must have shape (" #__VA_ARGS__ ")") | ||
|
||
void apply_rotary_cuda(const torch::Tensor x1, const torch::Tensor x2, | ||
const torch::Tensor cos, const torch::Tensor sin, | ||
torch::Tensor out1, torch::Tensor out2, | ||
const bool conj); | ||
|
||
void apply_rotary(const torch::Tensor x1, const torch::Tensor x2, | ||
const torch::Tensor cos, const torch::Tensor sin, | ||
torch::Tensor out1, torch::Tensor out2, | ||
const bool conj) { | ||
CHECK_DEVICE(x1); CHECK_DEVICE(x2); | ||
CHECK_DEVICE(cos); CHECK_DEVICE(sin); | ||
CHECK_DEVICE(out1); CHECK_DEVICE(out1); | ||
TORCH_CHECK(x1.dtype() == x2.dtype()); | ||
TORCH_CHECK(cos.dtype() == sin.dtype()); | ||
TORCH_CHECK(out1.dtype() == out2.dtype()); | ||
TORCH_CHECK(x1.dtype() == cos.dtype()); | ||
TORCH_CHECK(x1.dtype() == out1.dtype()); | ||
TORCH_CHECK(x1.sizes() == x2.sizes()); | ||
TORCH_CHECK(cos.sizes() == sin.sizes()); | ||
TORCH_CHECK(out1.sizes() == out2.sizes()); | ||
|
||
// Otherwise the kernel will be launched from cuda:0 device | ||
// Cast to char to avoid compiler warning about narrowing | ||
at::cuda::CUDAGuard device_guard{(char)x1.get_device()}; | ||
|
||
apply_rotary_cuda(x1, x2, cos, sin, out1, out2, conj); | ||
} | ||
|
||
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { | ||
m.def("apply_rotary", &apply_rotary, "Apply rotary embedding"); | ||
} |
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,41 @@ | ||
#include <torch/python.h> | ||
#include <ATen/native/TensorIterator.h> | ||
#include <ATen/native/cuda/Loops.cuh> | ||
|
||
void apply_rotary_cuda(const torch::Tensor x1, const torch::Tensor x2, | ||
const torch::Tensor cos, const torch::Tensor sin, | ||
torch::Tensor out1, torch::Tensor out2, | ||
const bool conj) { | ||
auto iter = at::TensorIteratorConfig() | ||
.add_output(out1) | ||
.add_output(out2) | ||
.add_input(x1) | ||
.add_input(x2) | ||
.add_input(cos) | ||
.add_input(sin) | ||
.check_all_same_dtype(false) | ||
.promote_inputs_to_common_dtype(false) | ||
.build(); | ||
|
||
if (!conj) { | ||
AT_DISPATCH_FLOATING_TYPES_AND2(at::kBFloat16, at::kHalf, x1.scalar_type(), "rotary_kernel", [&] { | ||
at::native::gpu_kernel_multiple_outputs( | ||
iter, [] GPU_LAMBDA (scalar_t x1, scalar_t x2, scalar_t cos, | ||
scalar_t sin) -> thrust::tuple<scalar_t, scalar_t> { | ||
scalar_t out1 = float(x1) * float(cos) - float(x2) * float(sin); | ||
scalar_t out2 = float(x1) * float(sin) + float(x2) * float(cos); | ||
return {out1, out2}; | ||
}); | ||
}); | ||
} else { | ||
AT_DISPATCH_FLOATING_TYPES_AND2(at::kBFloat16, at::kHalf, x1.scalar_type(), "rotary_kernel", [&] { | ||
at::native::gpu_kernel_multiple_outputs( | ||
iter, [] GPU_LAMBDA (scalar_t x1, scalar_t x2, scalar_t cos, | ||
scalar_t sin) -> thrust::tuple<scalar_t, scalar_t> { | ||
scalar_t out1 = float(x1) * float(cos) + float(x2) * float(sin); | ||
scalar_t out2 = -float(x1) * float(sin) + float(x2) * float(cos); | ||
return {out1, out2}; | ||
}); | ||
}); | ||
} | ||
} |
Oops, something went wrong.