Skip to content

Commit

Permalink
Add custom op Transpose2DCast
Browse files Browse the repository at this point in the history
  • Loading branch information
xadupre committed Jun 5, 2024
1 parent 1e8c121 commit 701bc3a
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 6 deletions.
7 changes: 6 additions & 1 deletion operators/cuda/cuda_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "cuda/add_mul.h"
#include "cuda/fast_gelu.h"
#include "cuda/negxplus1.h"
#incluce "cuda/transpose_cast.h"
#endif

FxLoadCustomOpFactory LoadCustomOpClasses_Contrib = []() -> CustomOpArray& {
Expand All @@ -17,6 +18,8 @@ FxLoadCustomOpFactory LoadCustomOpClasses_Contrib = []() -> CustomOpArray& {
#if ORT_API_VERSION >= 16
using AddSharedInputFloat16Type = typename contrib::AddOrMulSharedInput<ortc::MFloat16, true>;
using MulSharedInputFloat16Type = typename contrib::AddOrMulSharedInput<ortc::MFloat16, false>;
using Transpose2DCastFloat32ToFloat16Type = typename contrib::Transpose2DCast<float, ortc::MFloat16>;
using Transpose2DCastFloat16ToFloat32Type = typename contrib::Transpose2DCast<ortc::MFloat16, float>;
#endif


Expand All @@ -34,7 +37,9 @@ FxLoadCustomOpFactory LoadCustomOpClasses_Contrib = []() -> CustomOpArray& {
CustomCudaStructV2("FastGelu", contrib::FastGelu<ortc::MFloat16>),
CustomCudaStructV2("FastGelu", contrib::FastGelu<ortc::BFloat16>),
CustomCudaStructV2("MulSharedInput", MulSharedInputFloat16Type),
CustomCudaStructV2("NegXPlus1", contrib::NegXPlus1<ortc::MFloat16>)
CustomCudaStructV2("NegXPlus1", contrib::NegXPlus1<ortc::MFloat16>),
CustomCudaStructV2("Transpose2DCastFP16", Transpose2DCastFloat32ToFloat16Type),
CustomCudaStructV2("Transpose2DCastFP32", Transpose2DCastFloat16ToFloat32Type)
#endif
#endif
);
Expand Down
36 changes: 36 additions & 0 deletions operators/cuda/transpose_cast.h
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
51 changes: 51 additions & 0 deletions operators/cuda/transpose_cast_impl.cu
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);
}
9 changes: 9 additions & 0 deletions operators/cuda/transpose_cast_impl.cuh
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);
78 changes: 73 additions & 5 deletions test/cuda/test_cudaops.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ def _run(self, X):
return (1 - X,)


class Transpose2DCastFP16(OpRun):
op_domain = "ai.onnx.contrib"

def _run(self, X):
return (X.T.to(np.float16),)


class Transpose2DCastFP32(OpRun):
op_domain = "ai.onnx.contrib"

def _run(self, X):
return (X.T.to(np.float32),)


class TestCudaOps(unittest.TestCase):
@staticmethod
def _create_negpos_test_model(domain="ai.onnx.contrib"):
Expand Down Expand Up @@ -151,8 +165,6 @@ def test_cuda_negxplus1(self):
self._negxplus1_cuda(TensorProto.FLOAT16)

def _addmul_shared_input_cuda(self, itype, op_type, shapea=(3, 2, 3), shapeb=(3, 2, 3), shapec=(3, 2, 3)):
from onnx_extended.ortops.optim.cuda import get_ort_ext_libs

model1 = helper.make_model(
helper.make_graph(
[
Expand Down Expand Up @@ -181,7 +193,7 @@ def _addmul_shared_input_cuda(self, itype, op_type, shapea=(3, 2, 3), shapeb=(3,
f"{op_type}SharedInput",
["X", "Y", "Z"],
["XY", "XZ"],
domain="onnx_extended.ortops.optim.cuda",
domain="ai.onnx.contrib",
)
],
"nd",
Expand All @@ -197,7 +209,7 @@ def _addmul_shared_input_cuda(self, itype, op_type, shapea=(3, 2, 3), shapeb=(3,
),
opset_imports=[
helper.make_opsetid("", 18),
helper.make_opsetid("onnx_extended.ortops.optim.cuda", 1),
helper.make_opsetid("ai.onnx.contrib", 1),
],
ir_version=9,
)
Expand All @@ -212,7 +224,7 @@ def _addmul_shared_input_cuda(self, itype, op_type, shapea=(3, 2, 3), shapeb=(3,
expected = ref.run(None, feeds1)

opts = _ort.SessionOptions()
opts.register_custom_ops_library(get_ort_ext_libs()[0])
opts.register_custom_ops_library(_get_library_path())
sess = _ort.InferenceSession(model2.SerializeToString(), opts, providers=["CUDAExecutionProvider"])
got = sess.run(None, feeds1)
for i in range(2):
Expand Down Expand Up @@ -262,6 +274,62 @@ def test_add_shared_input_cuda_broadcast2(self):
shapec=(3, 2, 3),
)

def _transpose_cast_cuda(self, itype):
dtype = np.float32 if itype == TensorProto.FLOAT else np.float16
itype2 = TensorProto.FLOAT if itype == TensorProto.FLOAT16 else TensorProto.FLOAT16
model1 = helper.make_model(
helper.make_graph(
[
helper.make_node("Transpose", ["X"], ["t"], perm=[1, 0]),
helper.make_node("Cast", ["t"], ["Y"], to=itype2),
],
"nd",
[helper.make_tensor_value_info("X", itype, [None, None])],
[helper.make_tensor_value_info("Y", itype2, [None, None])],
),
opset_imports=[helper.make_opsetid("", 18)],
ir_version=9,
)

model2 = helper.make_model(
helper.make_graph(
[
helper.make_node(
("Transpose2DCastFP16" if itype2 == TensorProto.FLOAT16 else "Transpose2DCastFP32"),
["X"],
["Y"],
domain="ai.onnx.contrib",
)
],
"nd",
[helper.make_tensor_value_info("X", itype, [None, None])],
[helper.make_tensor_value_info("Y", itype2, [None, None])],
),
opset_imports=[
helper.make_opsetid("", 18),
helper.make_opsetid("ai.onnx.contrib", 1),
],
ir_version=9,
)

dtype = np.float32 if itype == TensorProto.FLOAT else np.float16
x = (np.arange(32 * 32 * 3) + 1).reshape((32, 32 * 3)).astype(dtype)

feeds1 = dict(X=x)
ref = ReferenceEvaluator(model1, new_ops=[Transpose2DCastFP16, Transpose2DCastFP32])
expected = ref.run(None, feeds1)[0]

opts = _ort.SessionOptions()
opts.register_custom_ops_library(_get_library_path())
sess = _ort.InferenceSession(model2.SerializeToString(), opts, providers=["CUDAExecutionProvider"])
got = sess.run(None, feeds1)[0]
self.assertEqualArray(expected, got, atol=1e-5)

@unittest.skipIf(not has_cuda(), reason="cuda not available")
def test_transpose_cast_cuda(self):
self._transpose_cast_cuda(TensorProto.FLOAT)
self._transpose_cast_cuda(TensorProto.FLOAT16)


if __name__ == "__main__":
unittest.main()

0 comments on commit 701bc3a

Please sign in to comment.