|
| 1 | +use super::{args::Args, ClipMeta}; |
| 2 | +use operators::{ |
| 3 | + conv::{self, Conv}, |
| 4 | + ByteOf, Hardware, LaunchError, Operator, QueueAlloc, QueueOf, TopoNode, |
| 5 | +}; |
| 6 | +use std::ops::{Deref, DerefMut}; |
| 7 | +use tensor::Tensor; |
| 8 | + |
| 9 | +pub trait Operators { |
| 10 | + type Hardware: Hardware; |
| 11 | + type TopoNode: TopoNode<Self::Hardware>; |
| 12 | + type Conv: Conv<Self::Hardware>; |
| 13 | +} |
| 14 | + |
| 15 | +pub trait WeightLoader { |
| 16 | + type Hardware: Hardware; |
| 17 | + type Weight<'s>: Deref<Target = [ByteOf<Self::Hardware>]> + 's |
| 18 | + where |
| 19 | + Self: 's; |
| 20 | + |
| 21 | + fn patch_embd<'a>(&'a self, queue: &'a QueueOf<Self::Hardware>) -> [Self::Weight<'a>; 2]; |
| 22 | +} |
| 23 | + |
| 24 | +pub struct ClipWorker<Ops: Operators, W> { |
| 25 | + meta: ClipMeta, |
| 26 | + weights: WeightDecorator<W>, |
| 27 | + conv: Ops::Conv, |
| 28 | + pub debug: bool, |
| 29 | +} |
| 30 | + |
| 31 | +impl<Ops: Operators, W> ClipWorker<Ops, W> { |
| 32 | + pub fn new(node: &Ops::TopoNode, meta: ClipMeta, weights: W) -> Self { |
| 33 | + let processor = node.processor(); |
| 34 | + Self { |
| 35 | + weights: meta.decorator(weights), |
| 36 | + meta, |
| 37 | + conv: Ops::Conv::new(processor), |
| 38 | + debug: true, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + #[inline] |
| 43 | + pub const fn meta(&self) -> &ClipMeta { |
| 44 | + &self.meta |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<Ops, W> ClipWorker<Ops, W> |
| 49 | +where |
| 50 | + Ops: Operators, |
| 51 | + W: WeightLoader<Hardware = Ops::Hardware>, |
| 52 | + ByteOf<Ops::Hardware>: 'static, |
| 53 | +{ |
| 54 | + pub fn launch<QA>( |
| 55 | + &mut self, |
| 56 | + args: Args<Ops::Hardware>, |
| 57 | + workspace: &mut [ByteOf<Ops::Hardware>], |
| 58 | + queue_alloc: &QA, |
| 59 | + ) -> Result<(), LaunchError> |
| 60 | + where |
| 61 | + QA: QueueAlloc<Hardware = Ops::Hardware>, |
| 62 | + { |
| 63 | + let Args { raw } = args; |
| 64 | + let queue = queue_alloc.queue(); |
| 65 | + |
| 66 | + let ClipMeta { dt_embd, .. } = self.meta; |
| 67 | + |
| 68 | + let [k, b] = self.weights.patch_embd(queue); |
| 69 | + let &[n, _, h, w] = raw.shape() else { |
| 70 | + unreachable!() |
| 71 | + }; |
| 72 | + let &[m, _, hk, wk] = k.shape() else { |
| 73 | + unreachable!() |
| 74 | + }; |
| 75 | + |
| 76 | + let mut embd = Tensor::new(dt_embd, &[n, m, h / hk, w / wk]).map(|s| queue_alloc.alloc(s)); |
| 77 | + self.conv(&mut embd, &raw, &k, &b, workspace, queue_alloc) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[allow(clippy::too_many_arguments)] |
| 82 | +impl<Ops, W> ClipWorker<Ops, W> |
| 83 | +where |
| 84 | + Ops: Operators, |
| 85 | + W: WeightLoader<Hardware = Ops::Hardware>, |
| 86 | +{ |
| 87 | + fn conv<Y, X, W_, B, QA>( |
| 88 | + &self, |
| 89 | + y: &mut Tensor<Y>, |
| 90 | + x: &Tensor<X>, |
| 91 | + w: &Tensor<W_>, |
| 92 | + b: &Tensor<B>, |
| 93 | + workspace: &mut [ByteOf<Ops::Hardware>], |
| 94 | + queue_alloc: &QA, |
| 95 | + ) -> Result<(), LaunchError> |
| 96 | + where |
| 97 | + Y: DerefMut<Target = [ByteOf<Ops::Hardware>]>, |
| 98 | + X: Deref<Target = [ByteOf<Ops::Hardware>]>, |
| 99 | + W_: Deref<Target = [ByteOf<Ops::Hardware>]>, |
| 100 | + B: Deref<Target = [ByteOf<Ops::Hardware>]>, |
| 101 | + QA: QueueAlloc<Hardware = Ops::Hardware>, |
| 102 | + { |
| 103 | + self.conv.launch( |
| 104 | + &conv::Args { |
| 105 | + y_layout: y.layout(), |
| 106 | + y_base: y.base_mut(), |
| 107 | + x_layout: x.layout(), |
| 108 | + x_base: x.base(), |
| 109 | + w_layout: w.layout(), |
| 110 | + w_base: w.base(), |
| 111 | + b_layout: b.layout(), |
| 112 | + b_base: b.base(), |
| 113 | + strides: [self.meta.d_patch; 2], |
| 114 | + dilations: [1; 2], |
| 115 | + pads: [0; 4], |
| 116 | + }, |
| 117 | + workspace, |
| 118 | + queue_alloc, |
| 119 | + ) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +struct WeightDecorator<W> { |
| 124 | + weights: W, |
| 125 | + patch_embd_w: Tensor<usize>, |
| 126 | + patch_embd_b: Tensor<usize>, |
| 127 | +} |
| 128 | + |
| 129 | +impl ClipMeta { |
| 130 | + fn decorator<W>(&self, weights: W) -> WeightDecorator<W> { |
| 131 | + WeightDecorator { |
| 132 | + patch_embd_w: self.patch_embd_w(), |
| 133 | + patch_embd_b: self.patch_embd_b(), |
| 134 | + weights, |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +impl<W: WeightLoader> WeightDecorator<W> { |
| 140 | + #[inline] |
| 141 | + pub fn patch_embd<'a>(&'a self, queue: &'a QueueOf<W::Hardware>) -> [Tensor<W::Weight<'a>>; 2] { |
| 142 | + let [w, b] = self.weights.patch_embd(queue); |
| 143 | + [ |
| 144 | + self.patch_embd_w.clone().map(|_| w), |
| 145 | + self.patch_embd_b.clone().map(|_| b), |
| 146 | + ] |
| 147 | + } |
| 148 | +} |
0 commit comments