-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathcreate.rs
616 lines (555 loc) · 21.3 KB
/
create.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
use std::{
any::Any,
ffi,
fmt::Debug,
marker::PhantomData,
ptr::{self, NonNull},
sync::Arc
};
#[cfg(feature = "ndarray")]
use ndarray::{ArcArray, Array, ArrayView, CowArray, Dimension};
use super::{DynTensor, Tensor};
use crate::{
error::assert_non_null_pointer,
memory::{Allocator, MemoryInfo},
ortsys,
tensor::{IntoTensorElementType, TensorElementType, Utf8Data},
value::ValueInner,
AllocatorType, DynValue, Error, MemoryType, Result, TensorRefMut, Value
};
impl DynTensor {
/// Construct a [`Value`] from an array of strings.
///
/// Just like numeric tensors, string tensor `Value`s can be created from:
/// - (with feature `ndarray`) a shared reference to a [`ndarray::CowArray`] (`&CowArray<'_, T, D>`);
/// - (with feature `ndarray`) a mutable/exclusive reference to an [`ndarray::ArcArray`] (`&mut ArcArray<T, D>`);
/// - (with feature `ndarray`) an owned [`ndarray::Array`];
/// - (with feature `ndarray`) a borrowed view of another array, as an [`ndarray::ArrayView`] (`ArrayView<'_, T,
/// D>`);
/// - a tuple of `(dimensions, data)` where:
/// * `dimensions` is one of `Vec<I>`, `[I]` or `&[I]`, where `I` is `i64` or `usize`;
/// * and `data` is one of `Vec<T>`, `Box<[T]>`, `Arc<Box<[T]>>`, or `&[T]`.
///
/// ```
/// # use ort::{Session, Value};
/// # fn main() -> ort::Result<()> {
/// # let session = Session::builder()?.commit_from_file("tests/data/vectorizer.onnx")?;
/// // You'll need to obtain an `Allocator` from a session in order to create string tensors.
/// let allocator = session.allocator();
///
/// // Create a string tensor from a raw data vector
/// let data = vec!["hello", "world"];
/// let value = Value::from_string_array(allocator, ([data.len()], data.into_boxed_slice()))?;
///
/// // Create a string tensor from an `ndarray::Array`
/// #[cfg(feature = "ndarray")]
/// let value = Value::from_string_array(
/// allocator,
/// ndarray::Array::from_shape_vec((1,), vec!["document".to_owned()]).unwrap()
/// )?;
/// # Ok(())
/// # }
/// ```
///
/// Note that string data will *always* be copied, no matter what form the data is provided in.
pub fn from_string_array<T: Utf8Data>(allocator: &Allocator, input: impl IntoValueTensor<Item = T>) -> Result<DynTensor> {
let mut value_ptr: *mut ort_sys::OrtValue = ptr::null_mut();
let (shape, data) = input.ref_parts()?;
let shape_ptr: *const i64 = shape.as_ptr();
let shape_len = shape.len();
// create tensor without data -- data is filled in later
ortsys![
unsafe CreateTensorAsOrtValue(allocator.ptr.as_ptr(), shape_ptr, shape_len as _, TensorElementType::String.into(), &mut value_ptr)
-> Error::CreateTensor;
nonNull(value_ptr)
];
// create null-terminated copies of each string, as per `FillStringTensor` docs
let null_terminated_copies: Vec<ffi::CString> = data
.iter()
.map(|elt| {
let slice = elt.as_utf8_bytes();
ffi::CString::new(slice)
})
.collect::<Result<Vec<_>, _>>()
.map_err(Error::FfiStringNull)?;
let string_pointers = null_terminated_copies.iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
ortsys![unsafe FillStringTensor(value_ptr, string_pointers.as_ptr(), string_pointers.len() as _) -> Error::FillStringTensor];
Ok(Value {
inner: ValueInner::RustOwned {
ptr: unsafe { NonNull::new_unchecked(value_ptr) },
_array: Box::new(()),
_memory_info: None
},
_markers: PhantomData
})
}
}
impl<T: IntoTensorElementType + Debug> Tensor<T> {
/// Construct a tensor [`Value`] in a given allocator with a given shape and datatype. The data contained in the
/// value will be zero-allocated on the allocation device.
///
/// This can be used to create a tensor with data on a certain device. For example, to create a tensor with pinned
/// (CPU) memory for use with CUDA:
/// ```no_run
/// # use ort::{Allocator, Session, Tensor, MemoryInfo, MemoryType, AllocationDevice, AllocatorType};
/// # fn main() -> ort::Result<()> {
/// # let session = Session::builder()?.commit_from_file("tests/data/upsample.onnx")?;
/// let allocator = Allocator::new(
/// &session,
/// MemoryInfo::new(AllocationDevice::CUDAPinned, 0, AllocatorType::Device, MemoryType::CPUInput)?
/// )?;
///
/// let mut img_input = Tensor::<f32>::new(&allocator, [1, 128, 128, 3])?;
/// # Ok(())
/// # }
/// ```
pub fn new(allocator: &Allocator, shape: impl ToDimensions) -> Result<Tensor<T>> {
let mut value_ptr: *mut ort_sys::OrtValue = ptr::null_mut();
let shape = shape.to_dimensions(None)?;
let shape_ptr: *const i64 = shape.as_ptr();
let shape_len = shape.len();
ortsys![
unsafe CreateTensorAsOrtValue(
allocator.ptr.as_ptr(),
shape_ptr,
shape_len as _,
T::into_tensor_element_type().into(),
&mut value_ptr
) -> Error::CreateTensorWithData;
nonNull(value_ptr)
];
Ok(Value {
inner: ValueInner::RustOwned {
ptr: unsafe { NonNull::new_unchecked(value_ptr) },
_array: Box::new(()),
_memory_info: None
},
_markers: PhantomData
})
}
/// Construct a tensor [`Value`] from an array of data.
///
/// Tensor `Value`s can be created from:
/// - (with feature `ndarray`) a shared reference to a [`ndarray::CowArray`] (`&CowArray<'_, T, D>`);
/// - (with feature `ndarray`) a mutable/exclusive reference to an [`ndarray::ArcArray`] (`&mut ArcArray<T, D>`);
/// - (with feature `ndarray`) an owned [`ndarray::Array`];
/// - (with feature `ndarray`) a borrowed view of another array, as an [`ndarray::ArrayView`] (`ArrayView<'_, T,
/// D>`);
/// - a tuple of `(dimensions, data)` where:
/// * `dimensions` is one of `Vec<I>`, `[I]` or `&[I]`, where `I` is `i64` or `usize`;
/// * and `data` is one of `Vec<T>`, `Box<[T]>`, `Arc<Box<[T]>>`, or `&[T]`.
///
/// ```
/// # use ort::Value;
/// # fn main() -> ort::Result<()> {
/// // Create a tensor from a raw data vector
/// let value = Value::from_array(([1usize, 2, 3], vec![1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0].into_boxed_slice()))?;
///
/// // Create a tensor from an `ndarray::Array`
/// #[cfg(feature = "ndarray")]
/// let value = Value::from_array(ndarray::Array4::<f32>::zeros((1, 16, 16, 3)))?;
/// # Ok(())
/// # }
/// ```
///
/// Creating string tensors requires a separate method; see [`Value::from_string_array`].
///
/// Note that data provided in an `ndarray` may be copied in some circumstances:
/// - `&CowArray<'_, T, D>` will always be copied regardless of whether it is uniquely owned or borrowed.
/// - `&mut ArcArray<T, D>` and `Array<T, D>` will be copied only if the data is not in a contiguous layout (which
/// is the case after most reshape operations)
/// - `ArrayView<'_, T, D>` will always be copied.
///
/// Raw data provided as a `Arc<Box<[T]>>`, `Box<[T]>`, or `Vec<T>` will never be copied. Raw data is expected to be
/// in standard, contigous layout.
pub fn from_array(input: impl IntoValueTensor<Item = T>) -> Result<Tensor<T>> {
let memory_info = MemoryInfo::new_cpu(AllocatorType::Arena, MemoryType::Default)?;
let mut value_ptr: *mut ort_sys::OrtValue = ptr::null_mut();
// f16 and bf16 are repr(transparent) to u16, so memory layout should be identical to onnxruntime
let (shape, ptr, ptr_len, guard) = input.into_parts()?;
let shape_ptr: *const i64 = shape.as_ptr();
let shape_len = shape.len();
let tensor_values_ptr: *mut std::ffi::c_void = ptr.cast();
assert_non_null_pointer(tensor_values_ptr, "TensorValues")?;
ortsys![
unsafe CreateTensorWithDataAsOrtValue(
memory_info.ptr.as_ptr(),
tensor_values_ptr,
(ptr_len * std::mem::size_of::<T>()) as _,
shape_ptr,
shape_len as _,
T::into_tensor_element_type().into(),
&mut value_ptr
) -> Error::CreateTensorWithData;
nonNull(value_ptr)
];
Ok(Value {
inner: ValueInner::RustOwned {
ptr: unsafe { NonNull::new_unchecked(value_ptr) },
_array: guard,
_memory_info: Some(memory_info)
},
_markers: PhantomData
})
}
}
impl<'a, T: IntoTensorElementType + Debug> TensorRefMut<'a, T> {
/// Create a mutable tensor view from a raw pointer and shape.
///
/// The length of data is determined by `T` and the given shape, so the given buffer must be at least
/// `shape.iter().product() * std::mem::size_of::<T>()` bytes.
///
/// This function can be used to create data from raw device memory, e.g. to directly provide data to an execution
/// provider. For instance, to create a tensor from a raw CUDA buffer using [`cudarc`](https://docs.rs/cudarc):
/// ```ignore
/// let device = CudaDevice::new(0)?;
/// let device_data = device.htod_sync_copy(&input_data)?;
///
/// let tensor: TensorRefMut<'_, f32> = unsafe {
/// TensorRefMut::from_raw(
/// MemoryInfo::new(AllocationDevice::CUDA, 0, AllocatorType::Device, MemoryType::Default)?,
/// (*device_data.device_ptr() as usize as *mut ()).cast(),
/// vec![1, 3, 512, 512]
/// )?
/// };
/// ```
///
/// # Safety
/// - The pointer must be valid for the device description provided by `MemoryInfo`.
/// - The returned tensor must outlive the data described by the data pointer.
pub unsafe fn from_raw(info: MemoryInfo, data: *mut ort_sys::c_void, shape: Vec<i64>) -> Result<TensorRefMut<'a, T>> {
let mut value_ptr: *mut ort_sys::OrtValue = ptr::null_mut();
// f16 and bf16 are repr(transparent) to u16, so memory layout should be identical to onnxruntime
let shape_ptr: *const i64 = shape.as_ptr();
let shape_len = shape.len();
let data_len = shape.iter().product::<i64>() as usize * std::mem::size_of::<T>();
ortsys![
unsafe CreateTensorWithDataAsOrtValue(
info.ptr.as_ptr(),
data,
data_len as _,
shape_ptr,
shape_len as _,
T::into_tensor_element_type().into(),
&mut value_ptr
) -> Error::CreateTensorWithData;
nonNull(value_ptr)
];
Ok(TensorRefMut::new(Value {
inner: ValueInner::CppOwned {
ptr: unsafe { NonNull::new_unchecked(value_ptr) },
drop: true,
_session: None
},
_markers: PhantomData
}))
}
}
pub trait IntoValueTensor {
type Item;
fn ref_parts(&self) -> Result<(Vec<i64>, &[Self::Item])>;
#[allow(clippy::type_complexity)]
fn into_parts(self) -> Result<(Vec<i64>, *mut Self::Item, usize, Box<dyn Any>)>;
}
pub trait ToDimensions {
fn to_dimensions(&self, expected_size: Option<usize>) -> Result<Vec<i64>>;
}
macro_rules! impl_to_dimensions {
(@inner) => {
fn to_dimensions(&self, expected_size: Option<usize>) -> Result<Vec<i64>> {
let v: Vec<i64> = self
.iter()
.enumerate()
.map(|(i, c)| if *c >= 1 { Ok(*c as i64) } else { Err(Error::InvalidDimension(i)) })
.collect::<Result<_>>()?;
let sum = v.iter().product::<i64>() as usize;
if let Some(expected_size) = expected_size {
if sum != expected_size {
Err(Error::TensorShapeMismatch {
input: v,
total: sum,
expected: expected_size
})
} else {
Ok(v)
}
} else {
Ok(v)
}
}
};
($(for $t:ty),+) => {
$(impl ToDimensions for $t {
impl_to_dimensions!(@inner);
})+
};
(<N> $(for $t:ty),+) => {
$(impl<const N: usize> ToDimensions for $t {
impl_to_dimensions!(@inner);
})+
};
}
impl_to_dimensions!(for &[usize], for &[i32], for &[i64], for Vec<usize>, for Vec<i32>, for Vec<i64>);
impl_to_dimensions!(<N> for [usize; N], for [i32; N], for [i64; N]);
#[cfg(feature = "ndarray")]
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<'i, 'v, T: Clone + 'static, D: Dimension + 'static> IntoValueTensor for &'i CowArray<'v, T, D>
where
'i: 'v
{
type Item = T;
fn ref_parts(&self) -> Result<(Vec<i64>, &[Self::Item])> {
let shape: Vec<i64> = self.shape().iter().map(|d| *d as i64).collect();
let data = self.as_slice().ok_or(Error::TensorDataNotContiguous)?;
Ok((shape, data))
}
fn into_parts(self) -> Result<(Vec<i64>, *mut Self::Item, usize, Box<dyn Any>)> {
// This will result in a copy in either form of the CowArray
let mut contiguous_array = self.as_standard_layout().into_owned();
let shape: Vec<i64> = contiguous_array.shape().iter().map(|d| *d as i64).collect();
let ptr = contiguous_array.as_mut_ptr();
let ptr_len = contiguous_array.len();
let guard = Box::new(contiguous_array);
Ok((shape, ptr, ptr_len, guard))
}
}
#[cfg(feature = "ndarray")]
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<T: Clone + 'static, D: Dimension + 'static> IntoValueTensor for &mut ArcArray<T, D> {
type Item = T;
fn ref_parts(&self) -> Result<(Vec<i64>, &[Self::Item])> {
let shape: Vec<i64> = self.shape().iter().map(|d| *d as i64).collect();
let data = self.as_slice().ok_or(Error::TensorDataNotContiguous)?;
Ok((shape, data))
}
fn into_parts(self) -> Result<(Vec<i64>, *mut Self::Item, usize, Box<dyn Any>)> {
if self.is_standard_layout() {
// We can avoid the copy here and use the data as is
let shape: Vec<i64> = self.shape().iter().map(|d| *d as i64).collect();
let ptr = self.as_mut_ptr();
let ptr_len = self.len();
let guard = Box::new(self.clone());
Ok((shape, ptr, ptr_len, guard))
} else {
// Need to do a copy here to get data in to standard layout
let mut contiguous_array = self.as_standard_layout().into_owned();
let shape: Vec<i64> = contiguous_array.shape().iter().map(|d| *d as i64).collect();
let ptr = contiguous_array.as_mut_ptr();
let ptr_len: usize = contiguous_array.len();
let guard = Box::new(contiguous_array);
Ok((shape, ptr, ptr_len, guard))
}
}
}
#[cfg(feature = "ndarray")]
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<T: Clone + 'static, D: Dimension + 'static> IntoValueTensor for Array<T, D> {
type Item = T;
fn ref_parts(&self) -> Result<(Vec<i64>, &[Self::Item])> {
let shape: Vec<i64> = self.shape().iter().map(|d| *d as i64).collect();
let data = self.as_slice().ok_or(Error::TensorDataNotContiguous)?;
Ok((shape, data))
}
fn into_parts(self) -> Result<(Vec<i64>, *mut Self::Item, usize, Box<dyn Any>)> {
if self.is_standard_layout() {
// We can avoid the copy here and use the data as is
let mut guard = Box::new(self);
let shape: Vec<i64> = guard.shape().iter().map(|d| *d as i64).collect();
let ptr = guard.as_mut_ptr();
let ptr_len = guard.len();
Ok((shape, ptr, ptr_len, guard))
} else {
// Need to do a copy here to get data in to standard layout
let mut contiguous_array = self.as_standard_layout().into_owned();
let shape: Vec<i64> = contiguous_array.shape().iter().map(|d| *d as i64).collect();
let ptr = contiguous_array.as_mut_ptr();
let ptr_len: usize = contiguous_array.len();
let guard = Box::new(contiguous_array);
Ok((shape, ptr, ptr_len, guard))
}
}
}
#[cfg(feature = "ndarray")]
impl<'v, T: Clone + 'static, D: Dimension + 'static> IntoValueTensor for ArrayView<'v, T, D> {
type Item = T;
fn ref_parts(&self) -> Result<(Vec<i64>, &[Self::Item])> {
let shape: Vec<i64> = self.shape().iter().map(|d| *d as i64).collect();
let data = self.as_slice().ok_or(Error::TensorDataNotContiguous)?;
Ok((shape, data))
}
fn into_parts(self) -> Result<(Vec<i64>, *mut Self::Item, usize, Box<dyn Any>)> {
// This will result in a copy in either form of the ArrayView
let mut contiguous_array = self.as_standard_layout().into_owned();
let shape: Vec<i64> = contiguous_array.shape().iter().map(|d| *d as i64).collect();
let ptr = contiguous_array.as_mut_ptr();
let ptr_len = contiguous_array.len();
let guard = Box::new(contiguous_array);
Ok((shape, ptr, ptr_len, guard))
}
}
impl<T: Clone + Debug + 'static, D: ToDimensions> IntoValueTensor for (D, &[T]) {
type Item = T;
fn ref_parts(&self) -> Result<(Vec<i64>, &[Self::Item])> {
let shape = self.0.to_dimensions(Some(self.1.len()))?;
Ok((shape, self.1))
}
fn into_parts(self) -> Result<(Vec<i64>, *mut Self::Item, usize, Box<dyn Any>)> {
let shape = self.0.to_dimensions(Some(self.1.len()))?;
let mut data = self.1.to_vec();
let ptr = data.as_mut_ptr();
let ptr_len: usize = data.len();
Ok((shape, ptr, ptr_len, Box::new(data)))
}
}
impl<T: Clone + Debug + 'static, D: ToDimensions> IntoValueTensor for (D, Vec<T>) {
type Item = T;
fn ref_parts(&self) -> Result<(Vec<i64>, &[Self::Item])> {
let shape = self.0.to_dimensions(Some(self.1.len()))?;
let data = &*self.1;
Ok((shape, data))
}
fn into_parts(mut self) -> Result<(Vec<i64>, *mut Self::Item, usize, Box<dyn Any>)> {
let shape = self.0.to_dimensions(Some(self.1.len()))?;
let ptr = self.1.as_mut_ptr();
let ptr_len: usize = self.1.len();
Ok((shape, ptr, ptr_len, Box::new(self.1)))
}
}
impl<T: Clone + Debug + 'static, D: ToDimensions> IntoValueTensor for (D, Box<[T]>) {
type Item = T;
fn ref_parts(&self) -> Result<(Vec<i64>, &[Self::Item])> {
let shape = self.0.to_dimensions(Some(self.1.len()))?;
let data = &*self.1;
Ok((shape, data))
}
fn into_parts(mut self) -> Result<(Vec<i64>, *mut Self::Item, usize, Box<dyn Any>)> {
let shape = self.0.to_dimensions(Some(self.1.len()))?;
let ptr = self.1.as_mut_ptr();
let ptr_len: usize = self.1.len();
Ok((shape, ptr, ptr_len, Box::new(self.1)))
}
}
impl<T: Clone + Debug + 'static, D: ToDimensions> IntoValueTensor for (D, Arc<Box<[T]>>) {
type Item = T;
fn ref_parts(&self) -> Result<(Vec<i64>, &[Self::Item])> {
let shape = self.0.to_dimensions(Some(self.1.len()))?;
let data = &*self.1;
Ok((shape, data))
}
fn into_parts(mut self) -> Result<(Vec<i64>, *mut Self::Item, usize, Box<dyn Any>)> {
let shape = self.0.to_dimensions(Some(self.1.len()))?;
let ptr = std::sync::Arc::<std::boxed::Box<[T]>>::make_mut(&mut self.1).as_mut_ptr();
let ptr_len: usize = self.1.len();
let guard = Box::new(Arc::clone(&self.1));
Ok((shape, ptr, ptr_len, guard))
}
}
#[cfg(feature = "ndarray")]
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<'i, 'v, T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<&'i CowArray<'v, T, D>> for Tensor<T>
where
'i: 'v
{
type Error = Error;
fn try_from(arr: &'i CowArray<'v, T, D>) -> Result<Self, Self::Error> {
Tensor::from_array(arr)
}
}
#[cfg(feature = "ndarray")]
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<'v, T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<ArrayView<'v, T, D>> for Tensor<T> {
type Error = Error;
fn try_from(arr: ArrayView<'v, T, D>) -> Result<Self, Self::Error> {
Tensor::from_array(arr)
}
}
#[cfg(feature = "ndarray")]
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<'i, 'v, T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<&'i CowArray<'v, T, D>> for DynTensor
where
'i: 'v
{
type Error = Error;
fn try_from(arr: &'i CowArray<'v, T, D>) -> Result<Self, Self::Error> {
Tensor::from_array(arr).map(|c| c.upcast())
}
}
#[cfg(feature = "ndarray")]
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<'v, T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<ArrayView<'v, T, D>> for DynTensor {
type Error = Error;
fn try_from(arr: ArrayView<'v, T, D>) -> Result<Self, Self::Error> {
Tensor::from_array(arr).map(|c| c.upcast())
}
}
#[cfg(feature = "ndarray")]
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<'i, 'v, T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<&'i CowArray<'v, T, D>> for DynValue
where
'i: 'v
{
type Error = Error;
fn try_from(arr: &'i CowArray<'v, T, D>) -> Result<Self, Self::Error> {
Tensor::from_array(arr).map(|c| c.into_dyn())
}
}
#[cfg(feature = "ndarray")]
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<'v, T: IntoTensorElementType + Debug + Clone + 'static, D: Dimension + 'static> TryFrom<ArrayView<'v, T, D>> for DynValue {
type Error = Error;
fn try_from(arr: ArrayView<'v, T, D>) -> Result<Self, Self::Error> {
Tensor::from_array(arr).map(|c| c.into_dyn())
}
}
macro_rules! impl_try_from {
(@T,I $($t:ty),+) => {
$(
impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<$t> for Tensor<T> {
type Error = Error;
fn try_from(value: $t) -> Result<Self, Self::Error> {
Tensor::from_array(value)
}
}
impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<$t> for DynTensor {
type Error = Error;
fn try_from(value: $t) -> Result<Self, Self::Error> {
Tensor::from_array(value).map(|c| c.upcast())
}
}
impl<T: IntoTensorElementType + Debug + Clone + 'static, I: ToDimensions> TryFrom<$t> for crate::DynValue {
type Error = Error;
fn try_from(value: $t) -> Result<Self, Self::Error> {
Tensor::from_array(value).map(|c| c.into_dyn())
}
}
)+
};
(@T,D $($t:ty),+) => {
$(
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<T: IntoTensorElementType + Debug + Clone + 'static, D: ndarray::Dimension + 'static> TryFrom<$t> for Tensor<T> {
type Error = Error;
fn try_from(value: $t) -> Result<Self, Self::Error> {
Tensor::from_array(value)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<T: IntoTensorElementType + Debug + Clone + 'static, D: ndarray::Dimension + 'static> TryFrom<$t> for DynTensor {
type Error = Error;
fn try_from(value: $t) -> Result<Self, Self::Error> {
Tensor::from_array(value).map(|c| c.upcast())
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
impl<T: IntoTensorElementType + Debug + Clone + 'static, D: ndarray::Dimension + 'static> TryFrom<$t> for crate::DynValue {
type Error = Error;
fn try_from(value: $t) -> Result<Self, Self::Error> {
Tensor::from_array(value).map(|c| c.into_dyn())
}
}
)+
};
}
#[cfg(feature = "ndarray")]
impl_try_from!(@T,D &mut ArcArray<T, D>, Array<T, D>);
impl_try_from!(@T,I (I, Arc<Box<[T]>>), (I, Vec<T>), (I, Box<[T]>), (I, &[T]));