#![allow(non_snake_case)]
use teeny_core::dtype::Float;
use teeny_macros::kernel;
use teeny_triton::triton::{
types::{AddOffsets, Comparison},
*,
};
#[kernel(backward = GeluBackward)]
pub fn gelu_forward<T: Triton, D: Float, const BLOCK_SIZE: i32>(
x_ptr: T::Pointer<D>,
y_ptr: T::Pointer<D>,
n_elements: i32,
) where
T::I32Tensor: types::Tensor<i32, 1>,
T::I32Tensor: Comparison<i32, BoolTensor = T::BoolTensor>,
T::Pointer<D>: AddOffsets<i32, 1, T::I32Tensor, Output = T::Tensor<T::Pointer<D>>>,
{
let pid = T::program_id(Axis::X);
let block_start = pid * BLOCK_SIZE;
let offsets = T::arange(0, BLOCK_SIZE) + block_start;
let in_bounds = offsets.lt(n_elements);
let x = T::load(
x_ptr.add_offsets(offsets),
Some(in_bounds),
None,
&[],
None,
None,
None,
false,
);
let one = T::full(&[BLOCK_SIZE], D::from_f64(1.0));
let neg2c = T::full(&[BLOCK_SIZE], D::from_f64(-2.0 * 0.7978845608028654));
let coeff = T::full(&[BLOCK_SIZE], D::from_f64(0.044715));
let inner = x + coeff * x * x * x;
let y = x / (one + T::exp(neg2c * inner));
T::store(
y_ptr.add_offsets(offsets),
y,
Some(in_bounds),
&[],
None,
None,
);
}
#[kernel]
pub fn gelu_backward<T: Triton, D: Float, const BLOCK_SIZE: i32>(
dy_ptr: T::Pointer<D>,
x_ptr: T::Pointer<D>,
dx_ptr: T::Pointer<D>,
n_elements: i32,
) where
T::I32Tensor: types::Tensor<i32, 1>,
T::I32Tensor: Comparison<i32, BoolTensor = T::BoolTensor>,
T::Pointer<D>: AddOffsets<i32, 1, T::I32Tensor, Output = T::Tensor<T::Pointer<D>>>,
{
let pid = T::program_id(Axis::X);
let block_start = pid * BLOCK_SIZE;
let offsets = T::arange(0, BLOCK_SIZE) + block_start;
let in_bounds = offsets.lt(n_elements);
let dy = T::load(
dy_ptr.add_offsets(offsets),
Some(in_bounds),
None,
&[],
None,
None,
None,
false,
);
let x = T::load(
x_ptr.add_offsets(offsets),
Some(in_bounds),
None,
&[],
None,
None,
None,
false,
);
let one = T::full(&[BLOCK_SIZE], D::from_f64(1.0));
let half = T::full(&[BLOCK_SIZE], D::from_f64(0.5));
let two = T::full(&[BLOCK_SIZE], D::from_f64(2.0));
let three = T::full(&[BLOCK_SIZE], D::from_f64(3.0));
let c = T::full(&[BLOCK_SIZE], D::from_f64(0.7978845608028654));
let neg2c = T::full(&[BLOCK_SIZE], D::from_f64(-2.0 * 0.7978845608028654));
let coeff = T::full(&[BLOCK_SIZE], D::from_f64(0.044715));
let inner = x + coeff * x * x * x;
let s = one / (one + T::exp(neg2c * inner)); let t = two * s - one; let sech2 = one - t * t; let dinner = c * (one + three * coeff * x * x);
let dx = dy * (half * (one + t) + x * half * sech2 * dinner);
T::store(
dx_ptr.add_offsets(offsets),
dx,
Some(in_bounds),
&[],
None,
None,
);
}
#[kernel(backward = MishBackward)]
pub fn mish_forward<T: Triton, D: Float, const BLOCK_SIZE: i32>(
x_ptr: T::Pointer<D>,
y_ptr: T::Pointer<D>,
n_elements: i32,
) where
T::I32Tensor: types::Tensor<i32, 1>,
T::I32Tensor: Comparison<i32, BoolTensor = T::BoolTensor>,
T::Pointer<D>: AddOffsets<i32, 1, T::I32Tensor, Output = T::Tensor<T::Pointer<D>>>,
{
let pid = T::program_id(Axis::X);
let block_start = pid * BLOCK_SIZE;
let offsets = T::arange(0, BLOCK_SIZE) + block_start;
let in_bounds = offsets.lt(n_elements);
let x = T::load(
x_ptr.add_offsets(offsets),
Some(in_bounds),
None,
&[],
None,
None,
None,
false,
);
let one = T::full(&[BLOCK_SIZE], D::from_f64(1.0));
let two = T::full(&[BLOCK_SIZE], D::from_f64(2.0));
let neg2 = T::full(&[BLOCK_SIZE], D::from_f64(-2.0));
let sp = T::log(one + T::exp(x)); let s2 = one / (one + T::exp(neg2 * sp));
let t = two * s2 - one;
let y = x * t;
T::store(
y_ptr.add_offsets(offsets),
y,
Some(in_bounds),
&[],
None,
None,
);
}
#[kernel]
pub fn mish_backward<T: Triton, D: Float, const BLOCK_SIZE: i32>(
dy_ptr: T::Pointer<D>,
x_ptr: T::Pointer<D>,
dx_ptr: T::Pointer<D>,
n_elements: i32,
) where
T::I32Tensor: types::Tensor<i32, 1>,
T::I32Tensor: Comparison<i32, BoolTensor = T::BoolTensor>,
T::Pointer<D>: AddOffsets<i32, 1, T::I32Tensor, Output = T::Tensor<T::Pointer<D>>>,
{
let pid = T::program_id(Axis::X);
let block_start = pid * BLOCK_SIZE;
let offsets = T::arange(0, BLOCK_SIZE) + block_start;
let in_bounds = offsets.lt(n_elements);
let dy = T::load(
dy_ptr.add_offsets(offsets),
Some(in_bounds),
None,
&[],
None,
None,
None,
false,
);
let x = T::load(
x_ptr.add_offsets(offsets),
Some(in_bounds),
None,
&[],
None,
None,
None,
false,
);
let one = T::full(&[BLOCK_SIZE], D::from_f64(1.0));
let two = T::full(&[BLOCK_SIZE], D::from_f64(2.0));
let neg1 = T::full(&[BLOCK_SIZE], D::from_f64(-1.0));
let neg2 = T::full(&[BLOCK_SIZE], D::from_f64(-2.0));
let sp = T::log(one + T::exp(x));
let s2 = one / (one + T::exp(neg2 * sp));
let t = two * s2 - one; let s = one / (one + T::exp(neg1 * x));
let dx = dy * (t + x * (one - t * t) * s);
T::store(
dx_ptr.add_offsets(offsets),
dx,
Some(in_bounds),
&[],
None,
None,
);
}
pub struct GeluOp<D: Float> {
pub forward: GeluForward<D>,
pub backward: GeluBackward<D>,
}
pub struct MishOp<D: Float> {
pub forward: MishForward<D>,
pub backward: MishBackward<D>,
}
impl<D: Float + Send + Sync + 'static> teeny_core::model::RuntimeOp for GeluForward<D> {
fn n_activation_inputs(&self) -> usize {
1
}
fn param_shapes(&self, _: &[&[usize]], _: &[usize]) -> Vec<Vec<usize>> {
Vec::new()
}
fn pack_args(
&self,
inputs: &[(teeny_core::model::RawPtr, &[usize])],
_params: &[teeny_core::model::RawPtr],
output: teeny_core::model::RawPtr,
output_shape: &[usize],
_output_row_stride: i32,
visitor: &mut dyn teeny_core::device::program::ArgVisitor,
) {
let n: usize = output_shape.iter().product();
visitor.visit_ptr(inputs[0].0);
visitor.visit_ptr(output);
visitor.visit_i32(n as i32);
}
fn block(&self) -> [u32; 3] {
[self.block_size as u32, 1, 1]
}
fn grid(&self, output_shape: &[usize]) -> [u32; 3] {
let n: usize = output_shape.iter().product();
[n.div_ceil(self.block_size as usize) as u32, 1, 1]
}
#[cfg(feature = "training")]
fn has_backward(&self) -> bool {
true
}
#[cfg(feature = "training")]
fn pack_backward_args(
&self,
inputs: &[(teeny_core::model::RawPtr, &[usize])],
_params: &[teeny_core::model::RawPtr],
_output: teeny_core::model::RawPtr,
output_shape: &[usize],
grad_output: teeny_core::model::RawPtr,
_grad_output_row_stride: i32,
grad_inputs: &[teeny_core::model::RawPtr],
_grad_params: &[teeny_core::model::RawPtr],
visitor: &mut dyn teeny_core::device::program::ArgVisitor,
) {
let n: usize = output_shape.iter().product();
visitor.visit_ptr(grad_output); visitor.visit_ptr(inputs[0].0); visitor.visit_ptr(grad_inputs[0]); visitor.visit_i32(n as i32);
}
#[cfg(feature = "training")]
fn backward_block(&self) -> [u32; 3] {
[self.block_size as u32, 1, 1]
}
#[cfg(feature = "training")]
fn backward_grid(&self, _: &[&[usize]], output_shape: &[usize]) -> [u32; 3] {
let n: usize = output_shape.iter().product();
[n.div_ceil(self.block_size as usize) as u32, 1, 1]
}
}