Swish-Gated Linear Unit
109 字
1 分钟
Swish-Gated Linear Unit
题面

答案
#include <cuda_runtime.h>
__global__ void swiglu_kernel(const float* input, float* output, int halfN) { int idx = blockDim.x * blockIdx.x + threadIdx.x; int half = N / 2; if (idx >= N) return; float x1 = input[idx]; float x2 = input[idx + half]; float sigma = 1.0 / (1.0 + exp(-x1)) float silu = x1 * sigma; float swiglu = silu * x2;}
// 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;
swiglu_kernel<<<blocksPerGrid, threadsPerBlock>>>(input, output, halfN); cudaDeviceSynchronize();}Swish-Gated Linear Unit
https://dongyanzhang.com/posts/leetgpu/swish-gated-linear-unit/


