Files
gpt-2-inference-engine/kernels_base/encoder.cuh
T
2026-06-07 13:43:20 -05:00

31 lines
954 B
Plaintext

#ifndef ENCODER_FORWARD_KERNEL_CUH
#define ENCODER_FORWARD_KERNEL_CUH
#include <cuda_runtime.h>
#include "../utils/cuda_utils.cuh"
__global__ void encoder_forward_kernel(float* out, const int* inp, const float* wte, const float* wpe,
int B, int T, int C) {
// Implement this
int x = blockDim.x * blockIdx.x + threadIdx.x;
int y = blockDim.y * blockIdx.y + threadIdx.y;
if(x < B && y < T){
int token = inp[x*T + y];
for(int i = 0; i < C; i ++){
out[x*T*C + y*C + i] = wte[token*C+i] + wpe[y*C+i];
}
}
}
// Launch kernel here
void encoder_forward(float* out, const int* inp, const float* wte, const float* wpe, int B, int T, int C) {
// Implement this
dim3 block(16,16,1);
dim3 grid((B + block.x -1)/ block.x,(T+block.y-1)/block.y);
encoder_forward_kernel<<<grid, block>>>(out, inp, wte, wpe,B,T,C);
}
#endif // ENCODER_FORWARD_KERNEL_CUH