use core::marker::PhantomData;
use teeny_core::dtype::Num;
use teeny_macros::kernel;
use teeny_triton::triton::{
types::{AddOffsets, Comparison},
*,
};
#[kernel]
pub fn channel_chunk_forward<T: Triton, D: Num, const BLOCK_SIZE: i32>(
x_ptr: T::Pointer<D>, y_ptr: T::Pointer<D>, c_total: i32, chunk_c: i32, chunk_offset: 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 num_c_tiles = T::cdiv(chunk_c, BLOCK_SIZE);
let pid_n = pid / num_c_tiles;
let ci_tile = pid % num_c_tiles;
let ci_start = ci_tile * BLOCK_SIZE;
let ci_offsets = T::arange(0, BLOCK_SIZE) + ci_start;
let in_bounds = ci_offsets.lt(chunk_c);
let in_offsets = ci_offsets + (pid_n * c_total + chunk_offset);
let out_offsets = ci_offsets + (pid_n * chunk_c);
let x = T::load(
x_ptr.add_offsets(in_offsets),
Some(in_bounds),
None,
&[],
None,
None,
None,
false,
);
T::store(
y_ptr.add_offsets(out_offsets),
x,
Some(in_bounds),
&[],
None,
None,
);
}
#[kernel]
pub fn channel_chunk_backward<T: Triton, D: Num, const BLOCK_SIZE: i32>(
dy_ptr: T::Pointer<D>, dx_ptr: T::Pointer<D>, c_total: i32,
chunk_c: i32,
chunk_offset: 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 num_c_tiles = T::cdiv(chunk_c, BLOCK_SIZE);
let pid_n = pid / num_c_tiles;
let ci_tile = pid % num_c_tiles;
let ci_start = ci_tile * BLOCK_SIZE;
let ci_offsets = T::arange(0, BLOCK_SIZE) + ci_start;
let in_bounds = ci_offsets.lt(chunk_c);
let dy_offsets = ci_offsets + (pid_n * chunk_c);
let dx_offsets = ci_offsets + (pid_n * c_total + chunk_offset);
let grad = T::load(
dy_ptr.add_offsets(dy_offsets),
Some(in_bounds),
None,
&[],
None,
None,
None,
false,
);
T::store(
dx_ptr.add_offsets(dx_offsets),
grad,
Some(in_bounds),
&[],
None,
None,
);
}
pub struct ChannelChunkOp<'a, D: Num> {
pub forward: ChannelChunkForward<D>,
pub backward: ChannelChunkBackward<D>,
_marker: PhantomData<&'a ()>,
}
pub struct ChannelChunkRuntimeOp<D: Num + Send + Sync + 'static> {
fwd: ChannelChunkForward<D>,
bwd: ChannelChunkBackward<D>,
chunk_c: usize,
chunk_offset: usize,
}
impl<D: Num + Send + Sync + 'static> ChannelChunkRuntimeOp<D> {
pub fn new(block_size: i32, chunk_c: usize, chunk_offset: usize) -> Self {
Self {
fwd: ChannelChunkForward::<D>::new(block_size),
bwd: ChannelChunkBackward::<D>::new(block_size),
chunk_c,
chunk_offset,
}
}
pub fn forward_source(&self) -> &str {
&self.fwd.source
}
pub fn backward_source(&self) -> &str {
&self.bwd.source
}
pub fn kernel_name(&self) -> &str {
self.fwd.name
}
}
impl<D: Num + Send + Sync + 'static> teeny_core::model::RuntimeOp for ChannelChunkRuntimeOp<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 input_shape = inputs[0].1;
let c_total = (input_shape[1] * input_shape[2] * input_shape[3]) as i32;
let chunk_c = self.chunk_c as i32;
let h = input_shape[2];
let w = input_shape[3];
let chunk_offset = (self.chunk_offset * h * w) as i32;
visitor.visit_ptr(inputs[0].0);
visitor.visit_ptr(output);
visitor.visit_i32(c_total);
visitor.visit_i32(chunk_c * (h as i32) * (w as i32)); visitor.visit_i32(chunk_offset);
}
fn block(&self) -> [u32; 3] {
[self.fwd.block_size as u32, 1, 1]
}
fn grid(&self, output_shape: &[usize]) -> [u32; 3] {
let n_spatial = output_shape[0];
let chunk_c_nc = output_shape[1] * output_shape[2] * output_shape[3];
let num_tiles = chunk_c_nc.div_ceil(self.fwd.block_size as usize);
[(n_spatial * num_tiles) 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 input_shape = inputs[0].1; let h = input_shape[2];
let w = input_shape[3];
let c_total = (input_shape[1] * h * w) as i32;
let chunk_c_nc = (self.chunk_c * h * w) as i32;
let chunk_offset = (self.chunk_offset * h * w) as i32;
visitor.visit_ptr(grad_output);
visitor.visit_ptr(grad_inputs[0]);
visitor.visit_i32(c_total);
visitor.visit_i32(chunk_c_nc);
visitor.visit_i32(chunk_offset);
}
#[cfg(feature = "training")]
fn backward_grid(&self, input_shapes: &[&[usize]], _output_shape: &[usize]) -> [u32; 3] {
let s = input_shapes[0];
let n_spatial = s[0];
let chunk_c_nc = self.chunk_c * s[2] * s[3];
let num_tiles = chunk_c_nc.div_ceil(self.fwd.block_size as usize);
[(n_spatial * num_tiles) as u32, 1, 1]
}
}