use burn::Tensor;
pub fn cat_optional<const D: usize>(
a: Option<Tensor<D>>,
b: Option<Tensor<D>>,
dim: usize,
) -> Option<Tensor<D>> {
match (a, b) {
(Some(a), Some(b)) => Some(Tensor::cat(vec![a, b], dim)),
(a, b) => a.or(b),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::support::testing::cpu_device;
#[test]
fn test_cat_optional() {
let device = cpu_device();
let a = Tensor::<2>::from_floats([[1.0, 2.0]], &device);
let b = Tensor::<2>::from_floats([[3.0, 4.0]], &device);
let both = cat_optional(Some(a.clone()), Some(b.clone()), 0).unwrap();
assert_eq!(both.dims(), [2, 2]);
let one = cat_optional(None, Some(b.clone()), 0).unwrap();
one.into_data().assert_eq(&b.into_data(), true);
assert!(cat_optional::<2>(None, None, 0).is_none());
}
}