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
5 changed files
with
175 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
#include "ocos.h" | ||
#include "transpose_cast_impl.cuh" | ||
#include "ortx_common.h" | ||
|
||
namespace contrib { | ||
|
||
template <typename TIN, typename TOUT> | ||
struct TransposeCast2D { | ||
template <typename TDict> | ||
OrtxStatus OnModelAttach(const TDict& /*dict*/) { | ||
return {}; | ||
} | ||
OrtxStatus Compute(Ort::Custom::CUDAKernelContext* ctx, | ||
const ortc::Tensor<TIN>& input, | ||
ortc::Tensor<TOUT>& output) const { | ||
const TIN* input_data = input.Data(); | ||
auto shape = input.Shape(); | ||
if (shape.size() != 2) | ||
ORTX_CXX_API_THROW("Input must be a 2D tensor", ORT_RUNTIME_EXCEPTION); | ||
size_t n_rows = shape[0]; | ||
size_t n_cols = shape[1]; | ||
TOUT* output_data = output.Allocate(shape); | ||
if (0 == n_rows || 0 == n_cols) { | ||
return {}; | ||
} | ||
TransposeCast2DKernel<TIN, TOUT>(reinterpret_cast<cudaStream_t>(ctx->GetCudaStream()), | ||
n_rows, n_cols, input_data, output_data); | ||
return {}; | ||
} | ||
}; | ||
|
||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "device_prop.cuh" | ||
#include "utils.cuh" | ||
#include "transpose_cast_impl.cuh" | ||
#include "cuda_type.h" | ||
|
||
using namespace Ort::Custom; | ||
|
||
template <typename TOUT, typename TIN> | ||
__global__ void TransposeCast2DKernel(TOUT *output_data, const TIN *input_data, int n_rows, int n_cols) { | ||
__shared__ TIN tile[TILE_DIM][TILE_DIM + 1]; | ||
|
||
int x = blockIdx.x * TILE_DIM + threadIdx.x; | ||
int y = blockIdx.y * TILE_DIM + threadIdx.y; | ||
// int width = gridDim.x * TILE_DIM; | ||
|
||
for (int j = 0; j < TILE_DIM; j += BLOCK_ROWS) | ||
tile[threadIdx.y + j][threadIdx.x] = input_data[(y + j) * n_cols + x]; | ||
|
||
__syncthreads(); | ||
|
||
x = blockIdx.y * TILE_DIM + threadIdx.x; // transpose block offset | ||
y = blockIdx.x * TILE_DIM + threadIdx.y; | ||
|
||
for (int j = 0; j < TILE_DIM; j += BLOCK_ROWS) | ||
output_data[(y + j) * n_rows + x] = (TOUT)(tile[threadIdx.x][threadIdx.y + j]); | ||
} | ||
|
||
template <typename TIN, typename TOUT> | ||
cudaError_t _LaunchTransposeCast2DKernel(cudaStream_t stream, size_t n_rows, size_t n_cols, const TIN* input, TOUT* output) { | ||
constexpr int blockSize = 256; | ||
const int gridSize = (input_length + blockSize - 1) / blockSize; | ||
using TTIN = typename contrib::CudaT<TIN>::MappedType; | ||
using TTOUT = typename contrib::CudaT<TOUT>::MappedType; | ||
TransposeCast2DKernel<TTOUT, TTIN><<<gridSize, blockSize, 0, stream>>>( | ||
reinterpret_cast<TTOUT*>(output), reinterpret_cast<const TTIN*>(input), | ||
static_cast<int>(n_rows), static_cast<int>(n_cols)); | ||
return cudaGetLastError(); | ||
} | ||
|
||
template <> | ||
cudaError_t LaunchTransposeCast2DKernel<float, ortc::MFloat16>(cudaStream_t stream, size_t n_rows, size_t n_cols, const float* input, ortc::MFloat16* output) { | ||
return _LaunchTransposeCast2DKernel(stream, n_rows, n_cols, , input, output); | ||
} | ||
|
||
template <> | ||
cudaError_t LaunchTransposeCast2DKernel<ortc::MFloat16, float>(cudaStream_t stream, size_t n_rows, size_t n_cols, const ortc::MFloat16* input, float* output) { | ||
return _LaunchTransposeCast2DKernel(stream, n_rows, n_cols, input, 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,9 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
#include <cuda.h> | ||
#include <cuda_runtime.h> | ||
|
||
template <typename TIN, typename TOUT> | ||
cudaError_t TransposeCast2DKernel(cudaStream_t stream, size_t n_rows, size_t n_cols, const TIN* input, TOUT* 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