#![allow(non_snake_case)]
use teeny_core::dtype::Float;
use teeny_macros::kernel;
use teeny_triton::triton::{
types::{AddOffsets, Comparison},
*,
};
#[kernel]
pub fn softmax_forward<T: Triton, D: Float, const BLOCK_SIZE: i32>(
x_ptr: T::Pointer<D>,
y_ptr: T::Pointer<D>,
_n_rows: i32,
n_cols: 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 row_offset = pid * n_cols;
let col_offsets = T::arange(0, BLOCK_SIZE);
let offsets = col_offsets + row_offset;
let x = T::load(
x_ptr.add_offsets(offsets),
None,
None,
&[],
None,
None,
None,
false,
);
let y = T::softmax(x, None, false, false);
T::store(y_ptr.add_offsets(offsets), y, None, &[], None, None);
}
#[kernel]
pub fn softmax_backward<T: Triton, D: Float, const BLOCK_SIZE: i32>(
dy_ptr: T::Pointer<D>,
y_ptr: T::Pointer<D>,
dx_ptr: T::Pointer<D>,
_n_rows: i32,
n_cols: 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 row_offset = pid * n_cols;
let col_offsets = T::arange(0, BLOCK_SIZE);
let offsets = col_offsets + row_offset;
let dy = T::load(
dy_ptr.add_offsets(offsets),
None,
None,
&[],
None,
None,
None,
false,
);
let y = T::load(
y_ptr.add_offsets(offsets),
None,
None,
&[],
None,
None,
None,
false,
);
let dot = T::sum(y * dy, Some(0), false);
let dx = y * (dy - dot);
T::store(dx_ptr.add_offsets(offsets), dx, None, &[], None, None);
}
impl<D: Float + Send + Sync + 'static> teeny_core::model::RuntimeOp for SoftmaxForward<D> {
fn n_activation_inputs(&self) -> usize {
1
}
fn param_shapes(&self, _input_shapes: &[&[usize]], _output_shape: &[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_rows = output_shape[0] as i32;
let n_cols = output_shape[1] as i32;
visitor.visit_ptr(inputs[0].0);
visitor.visit_ptr(output);
visitor.visit_i32(n_rows);
visitor.visit_i32(n_cols);
}
fn block(&self) -> [u32; 3] {
[128, 1, 1]
}
fn grid(&self, output_shape: &[usize]) -> [u32; 3] {
[output_shape[0] as u32, 1, 1]
}
}
pub struct SoftmaxOp<'a, T: Float> {
pub forward: SoftmaxForward<T>,
pub backward: SoftmaxBackward<T>,
_marker: core::marker::PhantomData<&'a ()>,
}