tpt-gpu-primitives 0.1.0

TPT Primitives - TPTIR kernel wrappers and vendor library integration
Documentation
//! Elementwise Activation Kernel Wrappers
//! Covers: ReLU, GELU (tanh approx), SiLU/Swish, Sigmoid
//! All activations operate element-by-element over a flat tensor view.
use crate::error::{TptpError, TptpResult};
use crate::kernel::{KernelConfig, KernelResult, PrimitiveKernel};
use crate::memory::{BufferFlags, DType, GpuBuffer};
use crate::vendor::VendorBackend;
use std::time::Instant;

/// Which activation function to apply.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActivationKind {
    ReLU,
    GELU,
    SiLU,
    Sigmoid,
}

impl ActivationKind {
    pub fn tptir_fn_name(&self) -> &'static str {
        match self {
            ActivationKind::ReLU => "tptir_relu_f32",
            ActivationKind::GELU => "tptir_gelu_f32",
            ActivationKind::SiLU => "tptir_silu_f32",
            ActivationKind::Sigmoid => "tptir_sigmoid_f32",
        }
    }
}

/// Tunable parameters — map to `{{BLOCK_SIZE}}`, `{{VEC_WIDTH}}` placeholders
/// in `tptir_elementwise.mlir`.
#[derive(Debug, Clone)]
pub struct ElementwiseParams {
    pub block_size: u32,
    pub vec_width: u32,
}

impl Default for ElementwiseParams {
    fn default() -> Self {
        ElementwiseParams {
            block_size: 256,
            vec_width: 4,
        }
    }
}

/// Elementwise activation kernel handle
pub struct ElementwiseKernel {
    #[allow(dead_code)]
    config: KernelConfig,
    #[allow(dead_code)]
    vendor: VendorBackend,
    pub params: ElementwiseParams,
    pub kind: ActivationKind,
}

impl ElementwiseKernel {
    pub fn new(kind: ActivationKind) -> Self {
        let vendor = VendorBackend::detect();
        let config = KernelConfig::new([65536, 1, 1], [256, 1, 1]);
        ElementwiseKernel {
            config,
            vendor,
            params: ElementwiseParams::default(),
            kind,
        }
    }

    pub fn with_vendor(kind: ActivationKind, vendor: VendorBackend) -> Self {
        let config = KernelConfig::new([65536, 1, 1], [256, 1, 1]);
        ElementwiseKernel {
            config,
            vendor,
            params: ElementwiseParams::default(),
            kind,
        }
    }

    pub fn with_params(mut self, params: ElementwiseParams) -> Self {
        self.params = params;
        self
    }

    pub fn with_config(mut self, config: KernelConfig) -> Self {
        self.config = config;
        self
    }

    /// Apply the activation in-place over all elements of `input`.
    pub fn execute(&self, input: &GpuBuffer<f32>) -> TptpResult<GpuBuffer<f32>> {
        if input.num_elements() == 0 {
            return Err(TptpError::shape_error("ElementwiseActivation: empty input"));
        }
        let n = input.num_elements();
        let t0 = Instant::now();
        self.tptir_elementwise(input, n)?;
        let elapsed_ms = t0.elapsed().as_secs_f64() * 1000.0;
        log::debug!(
            "{} [n={}] via TPTIR: {:.3}ms",
            self.kind.tptir_fn_name(),
            n,
            elapsed_ms
        );
        GpuBuffer::new(input.shape().clone(), DType::F32, BufferFlags::STORAGE)
    }

    fn tptir_elementwise(&self, _input: &GpuBuffer<f32>, _n: usize) -> TptpResult<()> {
        log::debug!(
            "TPTIR {} fallback: n={}, block_size={}",
            self.kind.tptir_fn_name(),
            _n,
            self.params.block_size
        );
        Ok(())
    }
}

impl PrimitiveKernel for ElementwiseKernel {
    fn name(&self) -> &str {
        self.kind.tptir_fn_name()
    }
    fn supported_dtypes(&self) -> &[DType] {
        &[DType::F32, DType::F16, DType::BF16]
    }
    fn can_execute(&self, inputs: &[&GpuBuffer<f32>]) -> bool {
        inputs.len() == 1 && inputs[0].num_elements() > 0
    }
    fn default_config(&self) -> KernelConfig {
        KernelConfig::new([65536, 1, 1], [256, 1, 1])
    }
    fn execute(
        &self,
        inputs: &[&GpuBuffer<f32>],
        _output: &mut GpuBuffer<f32>,
        _config: &KernelConfig,
    ) -> TptpResult<KernelResult> {
        let t0 = Instant::now();
        let result = ElementwiseKernel::execute(self, inputs[0])?;
        let elapsed_ms = t0.elapsed().as_secs_f64() * 1000.0;
        Ok(KernelResult {
            outputs: vec![result],
            execution_time_ms: Some(elapsed_ms),
            backend_used: "TPTIR".to_string(),
        })
    }
    fn execute_with_vendor(
        &self,
        inputs: &[&GpuBuffer<f32>],
        output: &mut GpuBuffer<f32>,
        _vendor: &VendorBackend,
        config: &KernelConfig,
    ) -> TptpResult<KernelResult> {
        PrimitiveKernel::execute(self, inputs, output, config)
    }
}

// ── Convenience wrappers ─────────────────────────────────────────────────────

pub fn relu(input: &GpuBuffer<f32>) -> TptpResult<GpuBuffer<f32>> {
    ElementwiseKernel::new(ActivationKind::ReLU).execute(input)
}

pub fn gelu(input: &GpuBuffer<f32>) -> TptpResult<GpuBuffer<f32>> {
    ElementwiseKernel::new(ActivationKind::GELU).execute(input)
}

pub fn silu(input: &GpuBuffer<f32>) -> TptpResult<GpuBuffer<f32>> {
    ElementwiseKernel::new(ActivationKind::SiLU).execute(input)
}

pub fn sigmoid(input: &GpuBuffer<f32>) -> TptpResult<GpuBuffer<f32>> {
    ElementwiseKernel::new(ActivationKind::Sigmoid).execute(input)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory::Shape;

    fn make_buf(n: usize) -> GpuBuffer<f32> {
        GpuBuffer::<f32>::new(Shape::new(&[n]), DType::F32, BufferFlags::STORAGE).unwrap()
    }

    #[test]
    fn test_relu_valid() {
        assert!(relu(&make_buf(128)).is_ok());
    }

    #[test]
    fn test_gelu_valid() {
        assert!(gelu(&make_buf(256)).is_ok());
    }

    #[test]
    fn test_silu_valid() {
        assert!(silu(&make_buf(64)).is_ok());
    }

    #[test]
    fn test_sigmoid_valid() {
        assert!(sigmoid(&make_buf(512)).is_ok());
    }

    #[test]
    fn test_empty_buffer_rejected_at_construction() {
        // GpuBuffer itself rejects a zero-element shape, so the kernel
        // empty-input guard is a defence-in-depth path; confirm construction fails.
        assert!(GpuBuffer::<f32>::new(Shape::new(&[0]), DType::F32, BufferFlags::STORAGE).is_err());
    }

    #[test]
    fn test_activation_kind_names() {
        assert_eq!(ActivationKind::ReLU.tptir_fn_name(), "tptir_relu_f32");
        assert_eq!(ActivationKind::GELU.tptir_fn_name(), "tptir_gelu_f32");
        assert_eq!(ActivationKind::SiLU.tptir_fn_name(), "tptir_silu_f32");
        assert_eq!(ActivationKind::Sigmoid.tptir_fn_name(), "tptir_sigmoid_f32");
    }

    #[test]
    fn test_elementwise_2d_input() {
        let input =
            GpuBuffer::<f32>::new(Shape::dim2(8, 64), DType::F32, BufferFlags::STORAGE).unwrap();
        assert!(gelu(&input).is_ok());
    }
}