Gaussian Error Gated Linear Unit
105 字
1 分钟
Gaussian Error Gated Linear Unit
题目

答案
#include <cuda_runtime.h>
__global__ void geglu_kernel(const float* input, float* output, int halfN) { int idx = blockDim.x * blockIdx.x + threadIdx.x; if (idx >= halfN) return;
float x1 = input[idx]; float x2 = input[idx + halfN];
const float inv_sqrt2 = 0.70710678118f;
float gelu = 0.5f * x2 * (1.0f + erff(x2 * inv_sqrt2));
output[idx] = x1 * gelu;}
// input, output are device pointersextern "C" void solve(const float* input, float* output, int N) { int halfN = N / 2; int threadsPerBlock = 256; int blocksPerGrid = (halfN + threadsPerBlock - 1) / threadsPerBlock;
geglu_kernel<<<blocksPerGrid, threadsPerBlock>>>(input, output, halfN); cudaDeviceSynchronize();}Gaussian Error Gated Linear Unit
https://dongyanzhang.com/posts/leetgpu/gaussian-error-gated-linear-unit/


