arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-hal/src/api/vmp_pmat.rs
poulpy-hal/src/api/vmp_pmat.rsRaw
 1use crate::layouts::{Backend, MatZnxToRef, Scratch, VecZnxDftToMut, VecZnxDftToRef, VmpPMatOwned, VmpPMatToMut, VmpPMatToRef};
 2
 3pub trait VmpPMatAlloc<B: Backend> {
 4    fn vmp_pmat_alloc(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> VmpPMatOwned<B>;
 5}
 6
 7pub trait VmpPMatAllocBytes {
 8    fn vmp_pmat_alloc_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize;
 9}
10
11pub trait VmpPMatFromBytes<B: Backend> {
12    fn vmp_pmat_from_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: Vec<u8>) -> VmpPMatOwned<B>;
13}
14
15pub trait VmpPrepareTmpBytes {
16    fn vmp_prepare_tmp_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize;
17}
18
19pub trait VmpPrepare<B: Backend> {
20    fn vmp_prepare<R, A>(&self, res: &mut R, a: &A, scratch: &mut Scratch<B>)
21    where
22        R: VmpPMatToMut<B>,
23        A: MatZnxToRef;
24}
25
26#[allow(clippy::too_many_arguments)]
27pub trait VmpApplyTmpBytes {
28    fn vmp_apply_tmp_bytes(
29        &self,
30        res_size: usize,
31        a_size: usize,
32        b_rows: usize,
33        b_cols_in: usize,
34        b_cols_out: usize,
35        b_size: usize,
36    ) -> usize;
37}
38
39pub trait VmpApply<B: Backend> {
40    /// Applies the vector matrix product [crate::layouts::VecZnxDft] x [crate::layouts::VmpPMat].
41    ///
42    /// A vector matrix product numerically equivalent to a sum of [crate::api::SvpApply],
43    /// where each [crate::layouts::SvpPPol] is a limb of the input [crate::layouts::VecZnx] in DFT,
44    /// and each vector a [crate::layouts::VecZnxDft] (row) of the [crate::layouts::VmpPMat].
45    ///
46    /// As such, given an input [crate::layouts::VecZnx] of `i` size and a [crate::layouts::VmpPMat] of `i` rows and
47    /// `j` size, the output is a [crate::layouts::VecZnx] of `j` size.
48    ///
49    /// If there is a mismatch between the dimensions the largest valid ones are used.
50    ///
51    /// ```text
52    /// |a b c d| x |e f g| = (a * |e f g| + b * |h i j| + c * |k l m|) = |n o p|
53    ///             |h i j|
54    ///             |k l m|
55    /// ```
56    /// where each element is a [crate::layouts::VecZnxDft].
57    ///
58    /// # Arguments
59    ///
60    /// * `c`: the output of the vector matrix product, as a [crate::layouts::VecZnxDft].
61    /// * `a`: the left operand [crate::layouts::VecZnxDft] of the vector matrix product.
62    /// * `b`: the right operand [crate::layouts::VmpPMat] of the vector matrix product.
63    /// * `buf`: scratch space, the size can be obtained with [VmpApplyTmpBytes::vmp_apply_tmp_bytes].
64    fn vmp_apply<R, A, C>(&self, res: &mut R, a: &A, b: &C, scratch: &mut Scratch<B>)
65    where
66        R: VecZnxDftToMut<B>,
67        A: VecZnxDftToRef<B>,
68        C: VmpPMatToRef<B>;
69}
70
71#[allow(clippy::too_many_arguments)]
72pub trait VmpApplyAddTmpBytes {
73    fn vmp_apply_add_tmp_bytes(
74        &self,
75        res_size: usize,
76        a_size: usize,
77        b_rows: usize,
78        b_cols_in: usize,
79        b_cols_out: usize,
80        b_size: usize,
81    ) -> usize;
82}
83
84pub trait VmpApplyAdd<B: Backend> {
85    fn vmp_apply_add<R, A, C>(&self, res: &mut R, a: &A, b: &C, scale: usize, scratch: &mut Scratch<B>)
86    where
87        R: VecZnxDftToMut<B>,
88        A: VecZnxDftToRef<B>,
89        C: VmpPMatToRef<B>;
90}