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 GELU layer for vision transformers #23219
Conversation
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.
Looks good!
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.
Thank you!
@rogday Could you review if possible? |
__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])) ); | ||
} |
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.
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.
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.
Okay
Add GELU layer for vision transformers * add gelu and gelu approximation * drop setKernelParams
Add GELU layer for vision transformers * add gelu and gelu approximation * drop setKernelParams
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.