1. 목적/책임
- ReLU, Sigmoid, Tanh, LeakyReLU, ELU, GELU(tanh 근사), SiLU(Swish) 등의 pointwise 활성화 연산을 제공합니다.
- FWD/BWD 커널을 노출하고, 필요 시 bias add를 FWD 경로에서 함께 처리합니다(옵션 인자 bias).
2. 지원 Op 식별자 (내부 enum)
// activation_ops.cuh
enum {
ACT_IDENTITY = 0,
ACT_RELU = 1,
ACT_SIGMOID = 2,
ACT_TANH = 3,
ACT_LEAKY = 4,
ACT_ELU = 5,
ACT_GELU = 6,
ACT_SILU = 7
};
3. 퍼블릭 API(런처)
void launch_activation_forward(const float* in, const float* bias, float* out,
int rows, int cols, int act_type,
float alpha, int gelu_tanh_flag,
cudaStream_t stream);
void launch_activation_backward(const float* grad_out,
const float* in, // pre-activation z
const float* out, // f(z)
float* grad_in,
int rows, int cols, int act_type,
float alpha, int gelu_tanh_flag,
cudaStream_t stream);
- bias는 선택(null 허용). 크기는 cols(per-sample 열 수)와 동일.
- alpha는 Leaky/ELU에 사용. 그 외 활성화에서는 무시.
- gelu_tanh_flag: 1=tanh 근사, 0=추후 precise(ERF) 분기 등 확장용.
- 그리드 구성은 (block= {ACT_BLOCK_X, ACT_BLOCK_Y}, grid={(cols+bx-1)/bx, (rows+by-1)/by}).
4. Shape 규약 (Per‑Sample)
- 입력/출력은 per‑sample 행렬(rows × cols)로 해석합니다.
- 전체 연산 요소 수: B*C, 여기서
- C = cols,
- B = batch_size * rows (시퀀스/채널 행을 rows로 둘 수 있음)
- Bias는 (1, C)를 가정하고 열 방향으로 브로드캐스트합니다.
5. 수치 안정성
- Sigmoid: 오버/언더플로우 방지를 위한 안정화 구현(sigmoid_stable). 출력은 [eps, 1-eps]로 클램프.
- Tanh/ELU: isfinite 가드 및 합리적 클램프.
- SiLU: y = x * sigmoid(x), 도함수 s + x*s*(1-s) 사용.
- GELU(tanh 근사): 0.5*x*(1+tanh(√(2/π)*(x+0.044715x^3))). BWD는 체인룰 기반.
- NaN/Inf 감지 시 0으로 대체하고 선택적 로깅(KPRINTF) 수행.
6. 메모리/레이아웃/성능
- Coalesced 접근: x축을 열(col)에, y축을 행(row)에 매핑(기본 블록: 32×8).
- 모든 포인터는 C‑contiguous float32를 가정합니다.
- In‑place 지원: out==in 가능(FWD). 스레드가 같은 인덱스를 읽고 동일 인덱스에 쓰므로 안전.
- BWD에서 grad_in==grad_out은 권장하지 않음(명시적 분리 권장).
- 향후 최적화: float2/float4 벡터화(열이 2/4 정렬), shared memory 타일링, -use_fast_math 플래그 검토.