Fused Operations Example
This example demonstrates how to fuse element-wise operations (bias and ReLU) with GEMM computation in a single kernel, improving performance by eliminating intermediate memory operations.Overview
Instead of computing GEMM and then applying bias and ReLU in separate kernel launches:Key Concepts
- Epilogue: The final stage of a GEMM kernel that processes the accumulator
- Fused operations: Combining multiple operations to reduce memory bandwidth
- Custom epilogue: Using CUTLASS epilogue templates for different output transformations
- Bias broadcasting: Applying per-row or per-column bias efficiently
Implementation
Building and Running
Build the example
Run the example
Source Code Location
The complete source code for this example is available at:examples/12_gemm_bias_relu/gemm_bias_relu.cu
What This Example Demonstrates
- Epilogue customization: Using custom epilogue operations for fused kernels
- Bias broadcasting: Efficiently applying per-row bias using stride-0 tensors
- Activation functions: Fusing ReLU activation with GEMM
- Performance optimization: Eliminating intermediate memory operations
- Mixed precision: Using FP16 inputs with FP32 accumulation and output
Performance Benefits
Fusing operations provides significant benefits:- Reduced memory bandwidth: No need to write/read intermediate results
- Lower latency: Single kernel launch instead of multiple
- Better cache utilization: Data stays in cache between operations
- Higher throughput: Overlapped computation and memory operations
Bias Layout Requirements
Important notes about bias layout:- For column-major output: bias must be M×1 (per-row bias)
- For row-major output: bias would be 1×N (per-column bias)
- Use stride 0 in the non-broadcast dimension to efficiently broadcast the bias vector
Key Takeaways
- CUTLASS epilogues enable efficient fusion of element-wise operations
LinearCombinationRelucombines scaling, bias addition, and ReLU activation- Bias broadcasting is achieved using stride-0 tensor references
- Fused operations significantly reduce memory traffic and improve performance
- The same pattern works for other epilogue operations (GELU, sigmoid, etc.)
Next Steps
- Explore Basic GEMM for simple matrix multiplication
- Learn about Batched GEMM for multiple independent operations
- Check out other epilogue operations in
cutlass/epilogue/thread/