Add GELU layer for vision transformers#23219
Merged
alalek merged 2 commits intoopencv:4.xfrom Feb 10, 2023
Merged
Conversation
Member
Author
|
@rogday Could you review if possible? |
alalek
reviewed
Feb 10, 2023
Comment on lines
+310
to
+324
| __kernel void GeluForward(const int n, __global T* in, __global T* out) | ||
| { | ||
| int index = get_global_id(0); | ||
| if(index < n) | ||
| out[index] = (T)0.5f * in[index] * ( (T)1.f + erf(in[index] * M_SQRT1_2) ); | ||
| } | ||
|
|
||
| __kernel void GeluApproximationForward(const int n, __global T* in, __global T* out, | ||
| const KERNEL_ARG_DTYPE sqrt_2_pi, | ||
| const KERNEL_ARG_DTYPE coef_sqrt_2_pi) | ||
| { | ||
| int index = get_global_id(0); | ||
| if(index < n) | ||
| out[index] = (T)0.5f * in[index] * ( (T)1.f + tanh(in[index] * (sqrt_2_pi + coef_sqrt_2_pi * in[index] * in[index])) ); | ||
| } |
Member
There was a problem hiding this comment.
Please use this OpenCL code:
__kernel void GeluForward(const int n, __global T* in, __global T* out)
{
int index = get_global_id(0);
if (index < n)
{
T x = in[index];
out[index] = (T)0.5f * x * ( (T)1.f + erf(x * M_SQRT1_2) );
}
}
__kernel void GeluApproximationForward(const int n, __global T* in, __global T* out)
{
// see GeluApproximationConstants from .cpp
const T sqrt_2_pi = 0.7978845834732056f;
const T coef_sqrt_2_pi = 0.044714998453855515f * sqrt_2_pi;
int index = get_global_id(0);
if(index < n)
{
T x = in[index];
out[index] = (T)0.5f * x * ( (T)1.f + tanh(x * (sqrt_2_pi + coef_sqrt_2_pi * x * x)) );
}
}
and drop setKernelParams() method.
a-sajjad72
pushed a commit
to a-sajjad72/opencv
that referenced
this pull request
Mar 30, 2023
Add GELU layer for vision transformers * add gelu and gelu approximation * drop setKernelParams
Merged
geversonsto
pushed a commit
to stodev-com-br/opencv
that referenced
this pull request
Jun 3, 2023
Add GELU layer for vision transformers * add gelu and gelu approximation * drop setKernelParams
48 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds the CPU and OCL kernels of GELU and GELU-Approximation layers.
Merge with opencv/opencv_extra#1044
References:
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.