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
6 changed files
with
187 additions
and
7 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
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. | ||
|
||
#pragma once | ||
#include "ocos.h" | ||
#include "transpose_cast_impl.cuh" | ||
#include "ortx_common.h" | ||
|
||
namespace contrib { | ||
|
||
template <typename TIN, typename TOUT> | ||
struct Transpose2DCast { | ||
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); | ||
} | ||
int n_rows = static_cast<int>(shape[0]); | ||
int n_cols = static_cast<int>(shape[1]); | ||
|
||
std::vector<int64_t> new_shape{static_cast<int64_t>(n_cols), static_cast<int64_t>(n_rows)}; | ||
TOUT* output_data = output.Allocate(new_shape); | ||
if (0 == n_rows || 0 == n_cols) { | ||
return {}; | ||
} | ||
LaunchTranspose2DCastKernel<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,56 @@ | ||
// 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; | ||
|
||
#define TILE_DIM 32 | ||
#define BLOCK_ROWS 8 | ||
|
||
template <typename TOUT, typename TIN> | ||
__global__ void Transpose2DCastKernel(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 _LaunchTranspose2DCastKernel(cudaStream_t stream, int n_rows, int n_cols, | ||
const TIN* input, TOUT* output) { | ||
dim3 dimGrid((n_cols + TILE_DIM - 1) / TILE_DIM, (n_rows + TILE_DIM - 1) / TILE_DIM, 1); | ||
dim3 dimBlock(TILE_DIM, BLOCK_ROWS, 1); | ||
using TTIN = typename contrib::CudaT<TIN>::MappedType; | ||
using TTOUT = typename contrib::CudaT<TOUT>::MappedType; | ||
Transpose2DCastKernel<TTOUT, TTIN><<<dimGrid, dimBlock, TILE_DIM * TILE_DIM + TILE_DIM, stream>>>( | ||
reinterpret_cast<TTOUT*>(output), reinterpret_cast<const TTIN*>(input), n_rows, n_cols); | ||
return cudaGetLastError(); | ||
} | ||
|
||
template <> | ||
cudaError_t LaunchTranspose2DCastKernel<float, ortc::MFloat16>(cudaStream_t stream, int n_rows, int n_cols, | ||
const float* input, ortc::MFloat16* output) { | ||
return _LaunchTranspose2DCastKernel(stream, n_rows, n_cols, input, output); | ||
} | ||
|
||
template <> | ||
cudaError_t LaunchTranspose2DCastKernel<ortc::MFloat16, float>(cudaStream_t stream, int n_rows, int n_cols, | ||
const ortc::MFloat16* input, float* output) { | ||
return _LaunchTranspose2DCastKernel(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 LaunchTranspose2DCastKernel(cudaStream_t stream, int n_rows, int 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