Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork56.4k
Add GELU layer for vision transformers#23219
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
alalek left a comment
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!
Uh oh!
There was an error while loading.Please reload this page.
alalek left a comment
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!
fengyuentau commentedFeb 9, 2023
@rogday Could you review if possible? |
| __kernelvoidGeluForward(constintn,__globalT*in,__globalT*out) | ||
| { | ||
| intindex=get_global_id(0); | ||
| if(index<n) | ||
| out[index]= (T)0.5f*in[index]* ( (T)1.f+erf(in[index]*M_SQRT1_2) ); | ||
| } | ||
| __kernelvoidGeluApproximationForward(constintn,__globalT*in,__globalT*out, | ||
| constKERNEL_ARG_DTYPEsqrt_2_pi, | ||
| constKERNEL_ARG_DTYPEcoef_sqrt_2_pi) | ||
| { | ||
| intindex=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 dropsetKernelParams() 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 withopencv/opencv_extra#1044
References:
Pull Request Readiness Checklist
See details athttps://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.