#![allow(non_snake_case)]
use teeny_core::dtype::Num;
use teeny_macros::kernel;
use teeny_triton::triton::{
types::{AddOffsets, Comparison, Tensor},
*,
};
#[kernel]
pub fn avgpool2d_forward<
T: Triton,
D: Num,
const KH: i32,
const KW: i32,
const STRIDE_H: i32,
const STRIDE_W: i32,
const BLOCK_OW: i32,
>(
input_ptr: T::Pointer<D>,
output_ptr: T::Pointer<D>,
_B: i32,
C: i32,
H: i32,
W: i32,
OH: i32,
OW: i32,
) where
T::I32Tensor: 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_ow_tiles = T::cdiv(OW, BLOCK_OW);
let ow_tile = pid % num_ow_tiles;
let bco = pid / num_ow_tiles;
let oh = bco % OH;
let bc = bco / OH;
let c = bc % C;
let b = bc / C;
let ow_start = ow_tile * BLOCK_OW;
let ow_range = T::arange(0, BLOCK_OW) + ow_start;
let ow_mask = ow_range.lt(OW);
let in_bc_base = (b * C + c) * H * W;
let out_bc_base = (b * C + c) * OH * OW;
let mut acc = T::zeros::<D>(&[BLOCK_OW]);
let loop_bound = KH * KW;
for idx in 0..loop_bound {
let kw = idx % KW;
let kh = idx / KW;
let ih = oh * STRIDE_H + kh;
let iw_range = ow_range * STRIDE_W + kw;
let in_offsets = iw_range + (in_bc_base + ih * W);
let tile = T::load(
input_ptr.add_offsets(in_offsets),
Some(ow_mask),
Some(T::zeros::<D>(&[BLOCK_OW])),
&[],
None,
None,
None,
false,
);
acc = acc + tile;
}
let ksize_1 = T::full::<i32>(&[1], KH * KW);
let ksize_f_1 = T::cast::<i32, D>(ksize_1, None, false);
let ksize = T::broadcast_to(ksize_f_1, &[BLOCK_OW]);
let result = acc / ksize;
let out_offsets = ow_range + (out_bc_base + oh * OW);
T::store(
output_ptr.add_offsets(out_offsets),
result,
Some(ow_mask),
&[],
None,
None,
);
}
#[kernel]
pub fn avgpool2d_backward<
T: Triton,
D: Num,
const KH: i32,
const KW: i32,
const STRIDE_H: i32,
const STRIDE_W: i32,
const BLOCK_OW: i32,
>(
dy_ptr: T::Pointer<D>,
dx_ptr: T::Pointer<D>,
_B: i32,
C: i32,
H: i32,
W: i32,
OH: i32,
OW: i32,
) where
T::I32Tensor: 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_ow_tiles = T::cdiv(OW, BLOCK_OW);
let ow_tile = pid % num_ow_tiles;
let bco = pid / num_ow_tiles;
let oh = bco % OH;
let bc = bco / OH;
let c = bc % C;
let b = bc / C;
let ow_start = ow_tile * BLOCK_OW;
let ow_range = T::arange(0, BLOCK_OW) + ow_start;
let ow_mask = ow_range.lt(OW);
let dy_bc_base = (b * C + c) * OH * OW;
let dx_bc_base = (b * C + c) * H * W;
let dy_offsets = ow_range + (dy_bc_base + oh * OW);
let dy_tile = T::load(
dy_ptr.add_offsets(dy_offsets),
Some(ow_mask),
Some(T::zeros::<D>(&[BLOCK_OW])),
&[],
None,
None,
None,
false,
);
let ksize_1 = T::full::<i32>(&[1], KH * KW);
let ksize_f_1 = T::cast::<i32, D>(ksize_1, None, false);
let ksize = T::broadcast_to(ksize_f_1, &[BLOCK_OW]);
let grad = dy_tile / ksize;
let loop_bound = KH * KW;
for idx in 0..loop_bound {
let kw = idx % KW;
let kh = idx / KW;
let ih = oh * STRIDE_H + kh;
let iw_range = ow_range * STRIDE_W + kw;
let dx_offsets = iw_range + (dx_bc_base + ih * W);
T::atomic_add(
dx_ptr.add_offsets(dx_offsets),
grad,
Some(ow_mask),
None,
None,
);
}
}
impl<D: Num + Send + Sync + 'static> teeny_core::model::RuntimeOp for Avgpool2dForward<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 input_shape = inputs[0].1;
visitor.visit_ptr(inputs[0].0);
visitor.visit_ptr(output);
visitor.visit_i32(input_shape[0] as i32); visitor.visit_i32(input_shape[1] as i32); visitor.visit_i32(input_shape[2] as i32); visitor.visit_i32(input_shape[3] as i32); visitor.visit_i32(output_shape[2] as i32); visitor.visit_i32(output_shape[3] as i32); }
fn block(&self) -> [u32; 3] {
[128, 1, 1]
}
fn grid(&self, output_shape: &[usize]) -> [u32; 3] {
let num_ow_tiles = output_shape[3].div_ceil(self.block_ow as usize);
[
(output_shape[0] * output_shape[1] * output_shape[2] * num_ow_tiles) as u32,
1,
1,
]
}
}
pub struct Avgpool2dOp<'a, T: Num> {
pub forward: Avgpool2dForward<T>,
pub backward: Avgpool2dBackward<T>,
_marker: core::marker::PhantomData<&'a ()>,
}