forked from microsoft/onnxruntime-extensions
-
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
4 changed files
with
165 additions
and
60 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,39 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "device_prop.cuh" | ||
#include "utils.cuh" | ||
#include "fast_gelu_impl.cuh" | ||
|
||
using namespace Ort::Custom; | ||
|
||
template <typename T> | ||
__device__ __inline__ T _neg1plusx(const T x) { | ||
return (T)1 - x; | ||
} | ||
|
||
template <> | ||
__device__ __inline__ half _neg1plusx(const half x) { | ||
#if __CUDA_ARCH__ < 700 | ||
return __float2half(1 - __half2float(x)); | ||
#else | ||
return (half)1 - x; | ||
#endif | ||
} | ||
|
||
template <typename T> | ||
__global__ void _NegXplus1Kernel(T* output_data, const T* input_data, int N) { | ||
int id = blockDim.x * blockIdx.x + threadIdx.x; | ||
if (id >= N) | ||
return; | ||
output_data[id] = _neg1plusx(input_data[id]); | ||
} | ||
|
||
template <typename T> | ||
cudaError_t LaunchNegXPlus1Kernel(cudaStream_t stream, int input_length, const T* input, T* output) { | ||
constexpr int blockSize = 256; | ||
const int gridSize = (input_length + blockSize - 1) / blockSize; | ||
using TT = typename CudaT<T>::MappedType; | ||
NegXPlus1Kernel<TT, blockSize><<<gridSize, blockSize, 0, stream>>>((TT*)input, (TT*)output, input_length); | ||
return cudaGetLastError(); | ||
} |
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,9 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
#include <cuda.h> | ||
#include <cuda_runtime.h> | ||
|
||
template <typename T> | ||
cudaError_t LaunchFastGeluKernel(cudaStream_t stream, int input_length, const T* input, T* output); |
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,35 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
#include "ocos.h" | ||
#include "negxplus1_impl.cuh" | ||
#include "cuda_type.h" | ||
|
||
namespace contrib { | ||
|
||
template <typename T> | ||
struct FastGelu { | ||
template <typename TDict> | ||
OrtStatusPtr OnModelAttach(const TDict& /*dict*/) { | ||
return nullptr; | ||
} | ||
OrtStatusPtr Compute(Ort::Custom::CUDAKernelContext* ctx, | ||
const ortc::Tensor<T>& input, | ||
ortc::Tensor<T>& output) const { | ||
const T* input_data = input.Data(); | ||
T* output_data = output.Allocate(input.Shape()); | ||
auto input_length = input.NumberOfElement(); | ||
if (0 == input_length) { | ||
return nullptr; | ||
} | ||
using TT = typename CudaT<T>::MappedType; | ||
LaunchNegXPlus1Kernel<TT>(reinterpret_cast<cudaStream_t>(ctx->GetCudaStream()), | ||
input_length, | ||
reinterpret_cast<const TT*>(input_data), | ||
reinterpret_cast<TT*>(output_data)); | ||
return nullptr; | ||
} | ||
}; | ||
|
||
} // namespace contrib |
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