-
Notifications
You must be signed in to change notification settings - Fork 7
/
tensor.rs
99 lines (85 loc) · 2.73 KB
/
tensor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
pub mod cv {
use crate::dlpack;
use crate::dlpack_py::{cvtensor_to_dlpack, cvtensor_to_dltensor};
use pyo3::prelude::*;
use std::ffi::c_void;
// in our case we don not want to delete the data
unsafe extern "C" fn deleter(_: *mut dlpack::DLManagedTensor) {
// println!("DLManagedTensor deleter");
//let ctx = (*x).manager_ctx as *mut Tensor;
//ctx.drop_in_place();
//(*x).dl_tensor.shape.drop_in_place();
//(*x).dl_tensor.strides.drop_in_place();
//x.drop_in_place();
}
fn get_strides_from_shape(shape: &[i64]) -> Vec<i64> {
let mut strides = vec![0i64; shape.len()];
let mut c = 1;
strides[shape.len() - 1] = c;
for i in (1..shape.len()).rev() {
c *= shape[i];
strides[i - 1] = c;
}
strides
}
#[pyclass]
#[derive(Debug, Clone, PartialEq)]
pub struct Tensor {
#[pyo3(get)]
pub shape: Vec<i64>,
#[pyo3(get)]
pub data: Vec<u8>,
#[pyo3(get)]
pub strides: Vec<i64>,
}
#[pymethods]
impl Tensor {
#[new]
pub fn new(shape: Vec<i64>, data: Vec<u8>) -> Self {
let strides = get_strides_from_shape(&shape);
Tensor {
shape,
data,
strides,
}
}
#[pyo3(name = "__dlpack__")]
pub fn to_dlpack_py(&self) -> PyResult<*mut pyo3::ffi::PyObject> {
cvtensor_to_dlpack(self)
}
#[pyo3(name = "__dlpack_device__")]
pub fn to_dlpack_device_py(&self) -> (u32, i32) {
let tensor_bx = Box::new(self);
let dl_tensor = cvtensor_to_dltensor(&tensor_bx);
(dl_tensor.device.device_type, dl_tensor.device.device_id)
}
}
impl Tensor {
pub fn to_dlpack(&self) -> dlpack::DLManagedTensor {
let tensor_bx = Box::new(self);
let dl_tensor = cvtensor_to_dltensor(&tensor_bx);
// create dlpack managed tensor
dlpack::DLManagedTensor {
dl_tensor,
manager_ctx: Box::into_raw(tensor_bx) as *mut c_void,
deleter: Some(deleter),
}
}
}
} // namespace cv
// TODO(carlos): enable tests later
//#[cfg(test)]
//mod tests {
// use super::*;
//
// #[test]
// fn add() {
// let shape: Vec<usize> = vec![1, 1, 2, 2];
// let data: Vec<u8> = (0..cv::cumprod(&shape)).map(|x| x as u8).collect();
// let t1 = cv::Tensor::new(shape.clone(), data);
// let t2 = t1.clone();
// let t3 = t1.add(t2.clone());
// let to_compare = cv::Tensor::new(shape.clone(), vec![0, 2, 4, 6]);
// assert_eq!(t3, to_compare);
// }
//}