-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom ops Rotary #738
Open
xadupre
wants to merge
13
commits into
microsoft:main
Choose a base branch
from
xadupre:rotary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1c9c4a4
draf
xadupre 52f351c
Fix implementation of Rotary
xadupre f67d3b1
fix merging conflicts
xadupre 29c460f
fix merge conflicts
xadupre ec3887a
comment
xadupre 7ba45f2
Merge branch 'main' of https://github.com/microsoft/onnxruntime-exten…
xadupre c406f42
fix merge conflicts
xadupre 1c535cb
fix merged conflicts
xadupre b67f62c
Merge branch 'main' of https://github.com/microsoft/onnxruntime-exten…
xadupre d70a588
Merge branch 'main' into rotary
xadupre ade1ae1
Merge branch 'rotary' of https://github.com/xadupre/onnxruntime-exten…
xadupre 9835fe8
Merge branch 'main' of https://github.com/microsoft/onnxruntime-exten…
xadupre dfeafa5
minor fixes
xadupre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,81 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
#include "ocos.h" | ||
#include "rotary_impl.cuh" | ||
#include "ortx_common.h" | ||
|
||
namespace contrib { | ||
|
||
/** | ||
* Y = Rotary(X) is equivalent to if side == LEFT: | ||
* | ||
* N = X.shape[-1] | ||
* Y = X.copy() | ||
* Y[...,:N/2] = X[...,N/2:] | ||
* Y[...,N/2:] = -X[...,:N/2] | ||
* | ||
* And the opposite if side == RIGHT: | ||
* | ||
* N = X.shape[-1] | ||
* Y = X.copy() | ||
* Y[...,:N/2] = -X[...,N/2:] | ||
* Y[...,N/2:] = X[...,:N/2] | ||
*/ | ||
template <typename T> | ||
struct Rotary { | ||
template <typename TDict> | ||
OrtxStatus OnModelAttach(const TDict& dict) { | ||
std::string empty; | ||
std::string side = dict.TryToGetAttributeWithDefault("side", empty); | ||
if (side == "left") { | ||
side_ = RotarySide::LEFT; | ||
} else if (side == "right") { | ||
side_ = RotarySide::RIGHT; | ||
} else { | ||
return {kOrtxErrorInvalidArgument, "side must be 'left' or 'right'."}; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
OrtxStatus Compute(Ort::Custom::CUDAKernelContext* ctx, const ortc::Tensor<T>& input, | ||
const ortc::Tensor<int64_t>& split, ortc::Tensor<T>& output) const { | ||
const T* input_data = input.Data(); | ||
auto input_shape = input.Shape(); | ||
T* output_data = output.Allocate(input_shape); | ||
auto input_length = input.NumberOfElement(); | ||
if (0 == input_length) { | ||
return {}; | ||
} | ||
|
||
auto shape_split = split.Shape(); | ||
if (shape_split.size() != 1 || shape_split[0] != 2) { | ||
wschin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return {kOrtxErrorInvalidArgument, "Rotary only works when there are two sides."}; | ||
} | ||
const int64_t* split_data = split.Data(); | ||
if (split_data[0] != split_data[1]) { | ||
return {kOrtxErrorInvalidArgument, "Only equal split is allowed."}; | ||
} | ||
if (split_data[0] != split_data[1] != input_shape[input_shape.size() - 1]) { | ||
return {kOrtxErrorInvalidArgument, "Sum of the splits are not equal to the last dimension."}; | ||
xadupre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
LaunchRotaryKernel<T>(reinterpret_cast<cudaStream_t>(ctx->GetCudaStream()), input_length, | ||
static_cast<int>(input_shape[input_shape.size() - 1]), input_data, split_data, output_data, | ||
side_); | ||
return {}; | ||
} | ||
|
||
static OrtMemType GetInputMemoryType(size_t input_index) { | ||
if (input_index == 1) // split | ||
return OrtMemType::OrtMemTypeCPUInput; | ||
return OrtMemType::OrtMemTypeDefault; | ||
} | ||
|
||
private: | ||
RotarySide side_; | ||
}; | ||
|
||
} // 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,81 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "device_prop.cuh" | ||
#include "utils.cuh" | ||
#include "rotary_impl.cuh" | ||
#include "cuda_type.h" | ||
|
||
#ifndef CUDA_LONG | ||
#define CUDA_LONG int32_t | ||
#endif | ||
|
||
using namespace Ort::Custom; | ||
|
||
template <typename T> __device__ __inline__ T _neg(const T x) { return -x; } | ||
|
||
#if __CUDA_ARCH__ < 700 | ||
template <> __device__ __inline__ half _neg(const half x) { | ||
return __float2half(-__half2float(x)); | ||
} | ||
#endif | ||
|
||
template <typename T, RotarySide side> | ||
xadupre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
__global__ void RotaryKernel(T *output_data, const T *input_data, CUDA_LONG half_N, CUDA_LONG half_stride) { | ||
CUDA_LONG id = blockDim.x * blockIdx.x + threadIdx.x; | ||
if (id >= half_N) | ||
return; | ||
CUDA_LONG last = id % half_stride; | ||
id = (id - last) * 2 + last; | ||
if (side == RotarySide::RIGHT) { | ||
output_data[id + half_stride] = input_data[id]; | ||
output_data[id] = _neg(input_data[id + half_stride]); | ||
} else { | ||
output_data[id + half_stride] = _neg(input_data[id]); | ||
output_data[id] = input_data[id + half_stride]; | ||
} | ||
} | ||
|
||
template <typename T> | ||
cudaError_t _LaunchRotaryKernel(cudaStream_t stream, int input_length, int last_dim, | ||
const T* input_data, const int64_t* /* split_data */, T* output_data, RotarySide side) { | ||
if (input_length == 0) | ||
return cudaGetLastError(); | ||
using TT = typename contrib::CudaT<T>::MappedType; | ||
|
||
CUDA_LONG N = static_cast<CUDA_LONG>(input_length); | ||
CUDA_LONG stride = static_cast<CUDA_LONG>(last_dim); | ||
|
||
const int num_threads_per_block = 256; | ||
const int num_elements_per_thread = | ||
(N / 2 + num_threads_per_block - 1) / num_threads_per_block; | ||
|
||
switch (side) { | ||
case RotarySide::LEFT: | ||
RotaryKernel<TT, RotarySide::LEFT> | ||
<<<num_elements_per_thread, num_threads_per_block, 0, stream>>>(reinterpret_cast<TT*>(output_data), | ||
reinterpret_cast<const TT*>(input_data), | ||
N / 2, stride / 2); | ||
break; | ||
case RotarySide::RIGHT: | ||
RotaryKernel<TT, RotarySide::RIGHT> | ||
<<<num_elements_per_thread, num_threads_per_block, 0, stream>>>(reinterpret_cast<TT*>(output_data), | ||
reinterpret_cast<const TT*>(input_data), | ||
N / 2, stride / 2); | ||
break; | ||
} | ||
return cudaGetLastError(); | ||
} | ||
|
||
template <> | ||
cudaError_t LaunchRotaryKernel<float>(cudaStream_t stream, int input_length, int last_dim, | ||
const float* input_data, const int64_t* split_data, float* output_data, RotarySide side) { | ||
return _LaunchRotaryKernel(stream, input_length, last_dim, input_data, split_data, output_data, side); | ||
} | ||
|
||
template <> | ||
cudaError_t LaunchRotaryKernel<ortc::MFloat16>(cudaStream_t stream, int input_length, int last_dim, | ||
const ortc::MFloat16* input_data, const int64_t* split_data, | ||
ortc::MFloat16* output_data, RotarySide side) { | ||
return _LaunchRotaryKernel(stream, input_length, last_dim, input_data, split_data, output_data, side); | ||
} |
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,15 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
#include <cuda.h> | ||
#include <cuda_runtime.h> | ||
|
||
enum class RotarySide : int { | ||
LEFT = 1, | ||
RIGHT = 2, | ||
}; | ||
|
||
template <typename T> | ||
cudaError_t LaunchRotaryKernel(cudaStream_t stream, int input_length, int last_dim, | ||
const T* input_data, const int64_t* split_data, T* output_data, RotarySide side); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should do
LeftRotary
andRightRotary
so that the compute function becomes stateless.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is done later here: https://github.com/microsoft/onnxruntime-extensions/pull/738/files#diff-643fdcb552aafbcb9a86bc8d48cab50dea3723b20b3105880e081b2e5b1b8da9R24. I hope the compiler is able to remove the unnecessary code at compilation time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It becomes more reasonable to split them in the beginning instead of relying on compiler. If they are two ops, we define two ops. No need to make a mega op to support several cases; this will make future improvement harder and error-prone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not the only place I did that. There is only one op from ort point of view and it is Rotary but we have different implementation depending the argument value, the same goes with the input types. Rotary(X, side=LEFT) = Neg(Rotary(X, side=RIGHT)). Do you want to have distinct onnx names as well?