|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "device_prop.cuh" |
| 5 | +#include "utils.cuh" |
| 6 | +#include "Rotary_impl.cuh" |
| 7 | +#include "cuda_type.h" |
| 8 | + |
| 9 | +using namespace Ort::Custom; |
| 10 | + |
| 11 | +template <typename T> __device__ __inline__ T _neg(const T x) { return -x; } |
| 12 | + |
| 13 | +#if __CUDA_ARCH__ < 700 |
| 14 | +template <> __device__ __inline__ half _neg(const half x) { |
| 15 | + return __float2half(-__half2float(x)); |
| 16 | +} |
| 17 | +#endif |
| 18 | + |
| 19 | +template <typename T, RotarySide side> |
| 20 | +__global__ void RotaryKernel(T *output_data, const T *input_data, CUDA_LONG half_N, CUDA_LONG half_stride) { |
| 21 | + CUDA_LONG id = blockDim.x * blockIdx.x + threadIdx.x; |
| 22 | + if (id >= half_N) |
| 23 | + return; |
| 24 | + CUDA_LONG last = id % half_stride; |
| 25 | + id = (id - last) * 2 + last; |
| 26 | + if (side == RotarySide::RIGHT) { |
| 27 | + output_data[id + half_stride] = input_data[id]; |
| 28 | + output_data[id] = _neg(input_data[id + half_stride]); |
| 29 | + } else { |
| 30 | + output_data[id + half_stride] = _neg(input_data[id]); |
| 31 | + output_data[id] = input_data[id + half_stride]; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +template <typename T> |
| 36 | +cudaError_t _LaunchRotaryKernel(cudaStream_t stream, int input_length, int last_dim, |
| 37 | + const T* input, const int64_t* split_data, T* output, RotarySide side) { |
| 38 | + constexpr int blockSize = 256; |
| 39 | + const int gridSize = (input_length + blockSize - 1) / blockSize; |
| 40 | + if (input_length == 0) |
| 41 | + return; |
| 42 | + using TT = typename contrib::CudaT<T>::MappedType; |
| 43 | + |
| 44 | + CUDA_LONG N = static_cast<CUDA_LONG>(count); |
| 45 | + CUDA_LONG stride = static_cast<CUDA_LONG>(last_dim); |
| 46 | + |
| 47 | + const int num_threads_per_block = GridDim::maxThreadsPerBlock; |
| 48 | + const int num_elements_per_thread = |
| 49 | + (N / 2 + num_threads_per_block - 1) / num_threads_per_block; |
| 50 | + |
| 51 | + switch (side) { |
| 52 | + case RotarySide::LEFT: |
| 53 | + RotaryKernel<T, RotarySide::LEFT> |
| 54 | + <<<num_elements_per_thread, num_threads_per_block, 0, stream>>>(output_data, input_data, |
| 55 | + N / 2, stride / 2); |
| 56 | + break; |
| 57 | + case RotarySide::RIGHT: |
| 58 | + RotaryKernel<T, RotarySide::RIGHT> |
| 59 | + <<<num_elements_per_thread, num_threads_per_block, 0, stream>>>(output_data, input_data, |
| 60 | + N / 2, stride / 2); |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + RotaryKernel<TT><<<gridSize, blockSize, 0, stream>>>(reinterpret_cast<TT*>(output), reinterpret_cast<const TT*>(input), input_length); |
| 65 | + return cudaGetLastError(); |
| 66 | +} |
| 67 | + |
| 68 | +template <> |
| 69 | +cudaError_t LaunchRotaryKernel<float>(cudaStream_t stream, int input_length, int last_dim, |
| 70 | + const float* input, const int64_t* split_data, float* output, RotarySide side) { |
| 71 | + return _LaunchRotaryKernel(stream, input_length, last_dim, input, split_data, output, side); |
| 72 | +} |
| 73 | + |
| 74 | +template <> |
| 75 | +cudaError_t LaunchRotaryKernel<ortc::MFloat16>(cudaStream_t stream, int input_length, int last_dim, |
| 76 | + const ortc::MFloat16* input, const int64_t* split_data, |
| 77 | + ortc::MFloat16* output, RotarySide side) { |
| 78 | + return _LaunchRotaryKernel(stream, input_length, last_dim, input, split_data, output, side); |
| 79 | +} |
0 commit comments