use burn::Tensor;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct BoundaryPermutes {
pub pre: Option<[usize; 3]>,
pub post: Option<[usize; 3]>,
}
impl BoundaryPermutes {
pub const IDENTITY: Self = Self {
pre: None,
post: None,
};
pub const SWAP_IN: Self = Self {
pre: Some([0, 2, 1]),
post: None,
};
pub fn pre_apply(
&self,
x: Tensor<3>,
) -> Tensor<3> {
match self.pre {
Some(order) => x.permute(order),
None => x,
}
}
pub fn post_apply(
&self,
y: Tensor<3>,
) -> Tensor<3> {
match self.post {
Some(order) => y.permute(order),
None => y,
}
}
}
#[cfg(test)]
mod tests {
use burn::tensor::Distribution;
use super::*;
use crate::support::testing::cpu_device;
#[test]
fn test_permutes() {
let device = cpu_device();
let x = Tensor::<3>::random([2, 3, 5], Distribution::Default, &device);
let same = BoundaryPermutes::IDENTITY.pre_apply(x.clone());
assert_eq!(same.dims(), [2, 3, 5]);
let swapped = BoundaryPermutes::SWAP_IN.pre_apply(x.clone());
assert_eq!(swapped.dims(), [2, 5, 3]);
swapped
.swap_dims(1, 2)
.to_data()
.assert_eq(&x.to_data(), true);
assert_eq!(
BoundaryPermutes::SWAP_IN.post_apply(x.clone()).dims(),
[2, 3, 5]
);
let order = BoundaryPermutes {
pre: None,
post: Some([1, 0, 2]),
};
assert_eq!(order.post_apply(x).dims(), [3, 2, 5]);
}
}