#pragma once #include // --- Warp-level reductions (no shared memory needed) --- __device__ __forceinline__ float warp_reduce_sum(float val) { #pragma unroll for (int offset = 16; offset > 0; offset >>= 1) val += __shfl_down_sync(0xffffffff, val, offset); return val; } __device__ __forceinline__ float warp_reduce_max(float val) { #pragma unroll for (int offset = 16; offset > 0; offset >>= 1) val = fmaxf(val, __shfl_down_sync(0xffffffff, val, offset)); return val; } // --- Block-level reductions --- __device__ __forceinline__ float block_reduce_sum(float val) { __shared__ float shared[32]; int lane = threadIdx.x & 31; int warp_id = threadIdx.x >> 5; int num_warps = (blockDim.x + 31) >> 5; val = warp_reduce_sum(val); if (lane == 0) shared[warp_id] = val; __syncthreads(); val = (threadIdx.x < num_warps) ? shared[threadIdx.x] : 0.0f; if (warp_id == 0) val = warp_reduce_sum(val); return val; } __device__ __forceinline__ float block_reduce_max(float val) { __shared__ float shared[32]; int lane = threadIdx.x & 31; int warp_id = threadIdx.x >> 5; int num_warps = (blockDim.x + 31) >> 5; val = warp_reduce_max(val); if (lane == 0) shared[warp_id] = val; __syncthreads(); val = (threadIdx.x < num_warps) ? shared[threadIdx.x] : -INFINITY; if (warp_id == 0) val = warp_reduce_max(val); return val; } // --- Launch error checking --- // Always on, including release builds. A launch with an invalid config // (e.g. 32-bit overflow in grid/index math) is otherwise silent and produces // garbage with no clue — the MoE int32-overflow bug was found exactly because // release swallowed the launch failure. `cudaGetLastError()` does not // synchronize the stream, so the per-launch host cost is negligible. #include #define CUDA_CHECK_LAST_ERROR() do { \ cudaError_t err = cudaGetLastError(); \ if (err != cudaSuccess) { \ fprintf(stderr, "CUDA kernel launch error at %s:%d: %s\n", \ __FILE__, __LINE__, cudaGetErrorString(err)); \ } \ } while(0)