|
| 1 | +use crate::{ |
| 2 | + get_static, rank_mismatch, shape_mismatch, shape_not_support, utils::type_distinct, ConstPtr, |
| 3 | + Hardware, MutPtr, SchemeError, TensorLayout, |
| 4 | +}; |
| 5 | +use digit_layout::DigitLayout; |
| 6 | +use itertools::izip; |
| 7 | +use std::{ |
| 8 | + cmp::Ordering, |
| 9 | + ptr::{null, null_mut}, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Clone)] |
| 13 | +pub struct Args<H: Hardware> { |
| 14 | + pub c_layout: TensorLayout, |
| 15 | + pub c_base: MutPtr<H>, |
| 16 | + pub a_layout: TensorLayout, |
| 17 | + pub a_base: ConstPtr<H>, |
| 18 | + pub s: f32, |
| 19 | +} |
| 20 | + |
| 21 | +impl<H: Hardware> Args<H> { |
| 22 | + pub fn new_null( |
| 23 | + c_layout: TensorLayout, |
| 24 | + a_layout: TensorLayout, |
| 25 | + b_layout: TensorLayout, |
| 26 | + ) -> Self { |
| 27 | + Self { |
| 28 | + c_layout, |
| 29 | + c_base: null_mut(), |
| 30 | + a_layout, |
| 31 | + a_base: null(), |
| 32 | + s: 1.0, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Clone, Debug)] |
| 38 | +pub(super) struct Scheme(DigitLayout, Box<[isize]>); |
| 39 | + |
| 40 | +impl Scheme { |
| 41 | + pub fn new<H: Hardware>(args: &Args<H>) -> Result<Self, SchemeError> { |
| 42 | + let Args { |
| 43 | + c_layout: c, |
| 44 | + a_layout: a, |
| 45 | + .. |
| 46 | + } = args; |
| 47 | + // # 检查基本属性 |
| 48 | + let dt = type_distinct(&[c.dt(), a.dt()])?; |
| 49 | + let ndim = c.ndim(); |
| 50 | + if a.ndim() != ndim { |
| 51 | + return Err(rank_mismatch(format!( |
| 52 | + "c.ndim = {}, a.ndim = {}", |
| 53 | + c.ndim(), |
| 54 | + a.ndim(), |
| 55 | + ))); |
| 56 | + } |
| 57 | + // # 输入形状 |
| 58 | + #[derive(Clone, PartialEq, Eq, Debug)] |
| 59 | + struct Dim { |
| 60 | + d: usize, |
| 61 | + c: isize, |
| 62 | + a: isize, |
| 63 | + } |
| 64 | + let mut dims = Vec::with_capacity(ndim); |
| 65 | + for (&d, &da, &sc, &sa) in izip!(c.shape(), a.shape(), c.strides(), a.strides(),) { |
| 66 | + get_static! { |
| 67 | + d da |
| 68 | + sc sa |
| 69 | + } |
| 70 | + if da != d { |
| 71 | + return Err(shape_mismatch(format!( |
| 72 | + "c: {:?}, a: {:?}", |
| 73 | + c.shape(), |
| 74 | + a.shape(), |
| 75 | + ))); |
| 76 | + } |
| 77 | + // 剔除初始的 1 长维度 |
| 78 | + if d != 1 { |
| 79 | + if sc == 0 { |
| 80 | + return Err(shape_not_support("Reducing is not allowed for scale")); |
| 81 | + } |
| 82 | + dims.push(Dim { d, c: sc, a: sa }) |
| 83 | + } |
| 84 | + } |
| 85 | + // # 排序 |
| 86 | + dims.sort_unstable_by(|dim0, dim1| { |
| 87 | + let &Dim { |
| 88 | + d: d0, |
| 89 | + c: c0, |
| 90 | + a: a0, |
| 91 | + } = dim0; |
| 92 | + let &Dim { |
| 93 | + d: d1, |
| 94 | + c: c1, |
| 95 | + a: a1, |
| 96 | + } = dim1; |
| 97 | + use Ordering::Equal as Eq; |
| 98 | + match c0.abs().cmp(&c1.abs()) { |
| 99 | + Eq => match a0.abs().cmp(&a1.abs()) { |
| 100 | + ord => ord.reverse(), |
| 101 | + }, |
| 102 | + ord => ord.reverse(), |
| 103 | + } |
| 104 | + }); |
| 105 | + // # 合并连续维度 |
| 106 | + let mut ndim = dims.len(); |
| 107 | + for i in (1..dims.len()).rev() { |
| 108 | + let (head, tail) = dims.split_at_mut(i); |
| 109 | + let f = &mut head[i - 1]; // f for front |
| 110 | + let b = &mut tail[0]; // b for back |
| 111 | + let d = b.d as isize; |
| 112 | + if b.c * d == f.c && b.a * d == f.a { |
| 113 | + *f = Dim { d: b.d * f.d, ..*b }; |
| 114 | + *b = Dim { d: 1, c: 0, a: 0 }; |
| 115 | + ndim -= 1 |
| 116 | + } |
| 117 | + } |
| 118 | + // # 合并空间 |
| 119 | + let mut layout = vec![0isize; 1 + ndim * 4].into_boxed_slice(); |
| 120 | + { |
| 121 | + let (idx, tail) = layout.split_at_mut(1 + ndim); |
| 122 | + let (c_, tail) = tail.split_at_mut(ndim); |
| 123 | + let (a_, b_) = tail.split_at_mut(ndim); |
| 124 | + for (Dim { d, c, a }, idx, c_, a_) in |
| 125 | + izip!(dims.into_iter().filter(|d| d.d != 1), &mut *idx, c_, a_) |
| 126 | + { |
| 127 | + *idx = d as _; |
| 128 | + *c_ = c; |
| 129 | + *a_ = a; |
| 130 | + } |
| 131 | + idx[ndim] = 1; |
| 132 | + for i in (1..=ndim).rev() { |
| 133 | + idx[i - 1] *= idx[i]; |
| 134 | + } |
| 135 | + } |
| 136 | + Ok(Self(dt, layout)) |
| 137 | + } |
| 138 | + |
| 139 | + #[inline] |
| 140 | + pub const fn dt(&self) -> DigitLayout { |
| 141 | + self.0 |
| 142 | + } |
| 143 | + |
| 144 | + /// 执行方案维数。 |
| 145 | + #[inline] |
| 146 | + pub fn ndim(&self) -> usize { |
| 147 | + (self.1.len() - 1) / 4 |
| 148 | + } |
| 149 | + |
| 150 | + /// 读写单元数量。 |
| 151 | + #[inline] |
| 152 | + pub fn count(&self) -> usize { |
| 153 | + self.1[0] as _ |
| 154 | + } |
| 155 | + |
| 156 | + /// 索引步长。 |
| 157 | + #[inline] |
| 158 | + pub fn idx_strides(&self) -> &[isize] { |
| 159 | + let ndim = self.ndim(); |
| 160 | + &self.1[1..][..ndim] |
| 161 | + } |
| 162 | + |
| 163 | + #[inline] |
| 164 | + pub fn c_strides(&self) -> &[isize] { |
| 165 | + let ndim = self.ndim(); |
| 166 | + &self.1[1 + ndim..][..ndim] |
| 167 | + } |
| 168 | + |
| 169 | + #[inline] |
| 170 | + pub fn a_strides(&self) -> &[isize] { |
| 171 | + let ndim = self.ndim(); |
| 172 | + &self.1[1 + ndim * 2..][..ndim] |
| 173 | + } |
| 174 | +} |
0 commit comments