Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

1.2 KB
Raw Blame
use uzu_engine_macros::uzu_config_abstract;

use crate::backends::common::gpu_types::activation_type::ActivationType;

pub mod gelu;
pub mod identity;
pub mod silu;

#[uzu_config_abstract(silu::SiLU, gelu::GELU, identity::Identity)]
pub struct Activation;

impl AnyActivation {
    pub fn act_type(&self) -> ActivationType {
        match self {
            AnyActivation::SiLU(_) => ActivationType::SILU,
            AnyActivation::GELU(gelu::GELU {
                approximate: true,
                ..
            }) => ActivationType::GELUApprox,
            AnyActivation::GELU(gelu::GELU {
                approximate: false,
                ..
            }) => ActivationType::GELUExact,
            AnyActivation::Identity(_) => ActivationType::IDENTITY,
        }
    }

    pub fn alpha(&self) -> f32 {
        match self {
            AnyActivation::SiLU(config) => config.alpha,
            AnyActivation::GELU(_) | AnyActivation::Identity(_) => 1.0,
        }
    }

    /// Non-default activation slope encoded by kernels that specialize it.
    pub fn custom_alpha(&self) -> Option<f32> {
        match self {
            AnyActivation::SiLU(config) if config.alpha != 1.0 => Some(config.alpha),
            _ => None,
        }
    }
}