Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Rust array to Tensor conversion. #790

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ image = { version = "0.24.5", optional = true }
clap = { version = "4.2.4", features = ["derive"], optional = true }
serde_json = { version = "1.0.96", optional = true }
memmap2 = { version = "0.6.1", optional = true }
slice-of-array = "0.3.2"

[dev-dependencies]
anyhow = "1"
Expand Down
58 changes: 58 additions & 0 deletions src/tensor/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,61 @@ impl<T: Element> TryFrom<Vec<T>> for Tensor {
Self::try_from(&value)
}
}

/// Create a tensor from a 1-dimensional array.
impl<T: Element, const N: usize> TryFrom<[T; N]> for Tensor {
type Error = TchError;

fn try_from(value: [T; N]) -> Result<Self, Self::Error> {
Self::f_from_slice(value.as_slice())
}
}

/// Create a tensor from a reference to a 1-dimensional array.
impl<T: Element, const N: usize> TryFrom<&[T; N]> for Tensor {
type Error = TchError;

fn try_from(value: &[T; N]) -> Result<Self, Self::Error> {
Self::f_from_slice(value.as_slice())
}
}

/// Create a tensor from a 2-dimensional array.
impl<T: Element, const N1: usize, const N2: usize> TryFrom<[[T; N2]; N1]> for Tensor {
type Error = TchError;

fn try_from(value: [[T; N2]; N1]) -> Result<Self, Self::Error> {
use ::slice_of_array::prelude::*;
let slice = value.as_slice().flat();
let tensor = Self::f_from_slice(slice)?;
tensor.f_view([N1 as i64, N2 as i64])
}
}

/// Create a tensor from a 3-dimensional array.
impl<T: Element, const N1: usize, const N2: usize, const N3: usize> TryFrom<[[[T; N3]; N2]; N1]>
for Tensor
{
type Error = TchError;

fn try_from(value: [[[T; N3]; N2]; N1]) -> Result<Self, Self::Error> {
use ::slice_of_array::prelude::*;
let slice = value.as_slice().flat().flat();
let tensor = Self::f_from_slice(slice)?;
tensor.f_view([N1 as i64, N2 as i64, N3 as i64])
}
}

/// Create a tensor from a 4-dimensional array.
impl<T: Element, const N1: usize, const N2: usize, const N3: usize, const N4: usize>
TryFrom<[[[[T; N4]; N3]; N2]; N1]> for Tensor
{
type Error = TchError;

fn try_from(value: [[[[T; N4]; N3]; N2]; N1]) -> Result<Self, Self::Error> {
use ::slice_of_array::prelude::*;
let slice = value.as_slice().flat().flat().flat();
let tensor = Self::f_from_slice(slice)?;
tensor.f_view([N1 as i64, N2 as i64, N3 as i64, N4 as i64])
}
}
105 changes: 105 additions & 0 deletions tests/tensor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,111 @@ fn from_slice() -> Result<()> {
Ok(())
}

#[test]
fn from_one_dimensional_array() -> Result<()> {
assert_eq!(vec_i32_from(&Tensor::try_from([-1_i32, 0, 1])?), vec![-1, 0, 1]);
assert_eq!(vec_i64_from(&Tensor::try_from([-1_i64, 0, 1])?), vec![-1, 0, 1]);
assert_eq!(
vec_f16_from(&Tensor::try_from([
f16::from_f64(-1.0),
f16::from_f64(0.0),
f16::from_f64(1.0)
])?),
vec![f16::from_f64(-1.0), f16::from_f64(0.0), f16::from_f64(1.0)]
);
assert_eq!(vec_f32_from(&Tensor::try_from([-1_f32, 0.0, 1.0])?), vec![-1.0, 0.0, 1.0]);
assert_eq!(vec_f64_from(&Tensor::try_from([-1_f64, 0.0, 1.0])?), vec![-1.0, 0.0, 1.0]);
assert_eq!(vec_bool_from(&Tensor::try_from([true, false])?), vec![true, false]);
Ok(())
}

#[test]
fn from_two_dimensional_array() -> Result<()> {
use ::slice_of_array::prelude::*;

let array = [[-1_i32, 0, 1], [-2, 0, 2]];
assert_eq!(vec_i32_from(&Tensor::try_from(array)?), array.flat());

let array = [[-1_i64, 0, 1], [-2, 0, 2]];
assert_eq!(vec_i64_from(&Tensor::try_from(array)?), array.flat());

let array = [
[f16::from_f64(-1.0), f16::from_f64(0.0), f16::from_f64(1.0)],
[f16::from_f64(-2.0), f16::from_f64(0.0), f16::from_f64(2.0)],
];
assert_eq!(vec_f16_from(&Tensor::try_from(array)?), array.flat());

let array = [[-1.0_f32, 0.0, 1.0], [-2.0, 0.0, 2.0]];
assert_eq!(vec_f32_from(&Tensor::try_from(array)?), array.flat());

let array = [[-1.0_f64, 0.0, 1.0], [-2.0, 0.0, 2.0]];
assert_eq!(vec_f64_from(&Tensor::try_from(array)?), array.flat());

Ok(())
}

#[test]
fn from_three_dimensional_array() -> Result<()> {
use ::slice_of_array::prelude::*;

let array = [[[-1_i32, 0, 1], [-2, 0, 2]], [[-3, 0, 3], [-4, 0, 4]]];
assert_eq!(vec_i32_from(&Tensor::try_from(array)?), array.flat().flat());

let array = [[[-1_i64, 0, 1], [-2, 0, 2]], [[-3, 0, 3], [-4, 0, 4]]];
assert_eq!(vec_i64_from(&Tensor::try_from(array)?), array.flat().flat());

let array = [
[
[f16::from_f64(-1.0), f16::from_f64(0.0), f16::from_f64(1.0)],
[f16::from_f64(-2.0), f16::from_f64(0.0), f16::from_f64(2.0)],
],
[
[f16::from_f64(-3.0), f16::from_f64(0.0), f16::from_f64(3.0)],
[f16::from_f64(-4.0), f16::from_f64(0.0), f16::from_f64(4.0)],
],
];
assert_eq!(vec_f16_from(&Tensor::try_from(array)?), array.flat().flat());

let array = [[[-1_f32, 0.0, 1.0], [-2.0, 0.0, 2.0]], [[-3.0, 0.0, 3.0], [-4.0, 0.0, 4.0]]];
assert_eq!(vec_f32_from(&Tensor::try_from(array)?), array.flat().flat());

let array = [[[-1_f64, 0.0, 1.0], [-2.0, 0.0, 2.0]], [[-3.0, 0.0, 3.0], [-4.0, 0.0, 4.0]]];
assert_eq!(vec_f64_from(&Tensor::try_from(array)?), array.flat().flat());

Ok(())
}

#[test]
fn from_four_dimensional_array() -> Result<()> {
use ::slice_of_array::prelude::*;

let array = [[[[-1_i32, 0, 1], [-2, 0, 2]], [[-3, 0, 3], [-4, 0, 4]]]];
assert_eq!(vec_i32_from(&Tensor::try_from(array)?), array.flat().flat().flat());

let array = [[[[-1_i64, 0, 1], [-2, 0, 2]], [[-3, 0, 3], [-4, 0, 4]]]];
assert_eq!(vec_i64_from(&Tensor::try_from(array)?), array.flat().flat().flat());

let array = [[
[
[f16::from_f64(-1.0), f16::from_f64(0.0), f16::from_f64(1.0)],
[f16::from_f64(-2.0), f16::from_f64(0.0), f16::from_f64(2.0)],
],
[
[f16::from_f64(-3.0), f16::from_f64(0.0), f16::from_f64(3.0)],
[f16::from_f64(-4.0), f16::from_f64(0.0), f16::from_f64(4.0)],
],
]];
assert_eq!(vec_f16_from(&Tensor::try_from(array)?), array.flat().flat().flat());

let array = [[[[-1_f32, 0.0, 1.0], [-2.0, 0.0, 2.0]], [[-3.0, 0.0, 3.0], [-4.0, 0.0, 4.0]]]];
assert_eq!(vec_f32_from(&Tensor::try_from(array)?), array.flat().flat().flat());

let array = [[[[-1_f64, 0.0, 1.0], [-2.0, 0.0, 2.0]], [[-3.0, 0.0, 3.0], [-4.0, 0.0, 4.0]]]];
assert_eq!(vec_f64_from(&Tensor::try_from(array)?), array.flat().flat().flat());

Ok(())
}

#[test]
fn test_device() {
let x = Tensor::from(1);
Expand Down
Loading