#![allow(non_snake_case)]
use teeny_macros::kernel;
use teeny_triton::triton::{
types::{AddOffsets, Comparison},
*,
};
#[kernel]
pub fn rprop_step<T: Triton, const BLOCK_SIZE: i32>(
params_ptr: T::Pointer<f32>,
grad_ptr: T::Pointer<f32>,
prev_grad_ptr: T::Pointer<f32>,
step_size_ptr: T::Pointer<f32>,
n_elements: i32,
eta_plus: f32,
eta_minus: f32,
step_min: f32,
step_max: f32,
) where
T::I32Tensor: types::Tensor<i32, 1>,
T::I32Tensor: Comparison<i32, BoolTensor = T::BoolTensor>,
T::Pointer<f32>: AddOffsets<i32, 1, T::I32Tensor, Output = T::Tensor<T::Pointer<f32>>>,
{
let pid = T::program_id(Axis::X);
let block_start = pid * BLOCK_SIZE;
let offsets = T::arange(0, BLOCK_SIZE) + block_start;
let mask = offsets.lt(n_elements);
let p = T::load(
params_ptr.add_offsets(offsets),
Some(mask),
None,
&[],
None,
None,
None,
false,
);
let g = T::load(
grad_ptr.add_offsets(offsets),
Some(mask),
None,
&[],
None,
None,
None,
false,
);
let prev_g = T::load(
prev_grad_ptr.add_offsets(offsets),
Some(mask),
None,
&[],
None,
None,
None,
false,
);
let step_size = T::load(
step_size_ptr.add_offsets(offsets),
Some(mask),
None,
&[],
None,
None,
None,
false,
);
let zeros = T::zeros::<f32>(&[BLOCK_SIZE]);
let ones = T::full(&[BLOCK_SIZE], 1.0_f32);
let neg_ones = T::full(&[BLOCK_SIZE], -1.0_f32);
let eta_plus_t = T::full(&[BLOCK_SIZE], eta_plus);
let eta_minus_t = T::full(&[BLOCK_SIZE], eta_minus);
let step_min_t = T::full(&[BLOCK_SIZE], step_min);
let step_max_t = T::full(&[BLOCK_SIZE], step_max);
let prod = g * prev_g;
let sign_pos = T::gt(prod, zeros); let sign_neg = T::lt(prod, zeros);
let step_after_pos = T::where_(sign_pos, step_size * eta_plus_t, step_size);
let step_scaled = T::where_(sign_neg, step_after_pos * eta_minus_t, step_after_pos);
let step_clamped = T::clamp(step_scaled, step_min_t, step_max_t);
let g_masked = T::where_(sign_neg, zeros, g);
let g_pos = T::gt(g_masked, zeros);
let g_neg = T::lt(g_masked, zeros);
let g_sign = T::where_(g_pos, ones, T::where_(g_neg, neg_ones, zeros));
let p_new = p - g_sign * step_clamped;
T::store(
params_ptr.add_offsets(offsets),
p_new,
Some(mask),
&[],
None,
None,
);
T::store(
step_size_ptr.add_offsets(offsets),
step_clamped,
Some(mask),
&[],
None,
None,
);
T::store(
prev_grad_ptr.add_offsets(offsets),
g_masked,
Some(mask),
&[],
None,
None,
);
}