Struct ort::ValueRefMut
source · pub struct ValueRefMut<'v, Type: ValueTypeMarker + ?Sized = DynValueTypeMarker> { /* private fields */ }
Expand description
A mutable temporary version of a Value
with a lifetime specifier.
Implementations§
source§impl<'a, T: IntoTensorElementType + Debug> ValueRefMut<'a, TensorValueType<T>>
impl<'a, T: IntoTensorElementType + Debug> ValueRefMut<'a, TensorValueType<T>>
sourcepub unsafe fn from_raw(
info: MemoryInfo,
data: *mut c_void,
shape: Vec<i64>
) -> Result<TensorRefMut<'a, T>>
pub unsafe fn from_raw( info: MemoryInfo, data: *mut c_void, shape: Vec<i64> ) -> Result<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
:
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.
source§impl<'v, Type: ValueTypeMarker + ?Sized> ValueRefMut<'v, Type>
impl<'v, Type: ValueTypeMarker + ?Sized> ValueRefMut<'v, Type>
pub fn into_dyn(self) -> ValueRefMut<'v, DynValueTypeMarker>
Methods from Deref<Target = Value<Type>>§
pub fn try_extract_map<K: IntoTensorElementType + Clone + Hash + Eq, V: IntoTensorElementType + Clone>( &self, allocator: &Allocator ) -> Result<HashMap<K, V>>
pub fn extract_map(&self, allocator: &Allocator) -> HashMap<K, V>
sourcepub fn upcast_ref(&self) -> DynMapRef<'_>
pub fn upcast_ref(&self) -> DynMapRef<'_>
sourcepub fn upcast_mut(&mut self) -> DynMapRefMut<'_>
pub fn upcast_mut(&mut self) -> DynMapRefMut<'_>
pub fn try_extract_sequence<'s, OtherType: ValueTypeMarker + DowncastableTarget + Debug + Sized>( &'s self, allocator: &Allocator ) -> Result<Vec<ValueRef<'s, OtherType>>>
pub fn extract_sequence<'s>( &'s self, allocator: &Allocator ) -> Vec<ValueRef<'s, T>>
sourcepub fn upcast_ref(&self) -> DynSequenceRef<'_>
pub fn upcast_ref(&self) -> DynSequenceRef<'_>
Converts from a strongly-typed Sequence<T>
to a reference to a type-erased DynSequence
.
sourcepub fn upcast_mut(&mut self) -> DynSequenceRefMut<'_>
pub fn upcast_mut(&mut self) -> DynSequenceRefMut<'_>
Converts from a strongly-typed Sequence<T>
to a mutable reference to a type-erased DynSequence
.
sourcepub fn try_extract_tensor<T: IntoTensorElementType>(
&self
) -> Result<ArrayViewD<'_, T>>
Available on crate feature ndarray
only.
pub fn try_extract_tensor<T: IntoTensorElementType>( &self ) -> Result<ArrayViewD<'_, T>>
ndarray
only.Attempt to extract the underlying data of type T
into a read-only ndarray::ArrayView
.
See also:
- the mutable counterpart of this function,
Tensor::try_extract_tensor_mut
. - the infallible counterpart,
Tensor::extract_tensor
, for typedTensor<T>
s. - the alternative function for strings,
Tensor::try_extract_string_tensor
.
let array = ndarray::Array4::<f32>::ones((1, 16, 16, 3));
let value = Value::from_array(array.view())?;
let extracted = value.try_extract_tensor::<f32>()?;
assert_eq!(array.into_dyn(), extracted);
§Errors
May return an error if:
- This is a
crate::DynValue
, and the value is not actually a tensor. (for typedTensor
s, use the infallibleTensor::extract_tensor
instead) - The provided type
T
does not match the tensor’s element type.
sourcepub fn try_extract_tensor_mut<T: IntoTensorElementType>(
&mut self
) -> Result<ArrayViewMutD<'_, T>>
Available on crate feature ndarray
only.
pub fn try_extract_tensor_mut<T: IntoTensorElementType>( &mut self ) -> Result<ArrayViewMutD<'_, T>>
ndarray
only.Attempt to extract the underlying data of type T
into a mutable read-only ndarray::ArrayViewMut
.
See also the infallible counterpart, Tensor::extract_tensor_mut
, for typed Tensor<T>
s.
let array = ndarray::Array4::<f32>::ones((1, 16, 16, 3));
let mut value = Value::from_array(array.view())?;
let mut extracted = value.try_extract_tensor_mut::<f32>()?;
extracted[[0, 0, 0, 1]] = 0.0;
let mut array = array.into_dyn();
assert_ne!(array, extracted);
array[[0, 0, 0, 1]] = 0.0;
assert_eq!(array, extracted);
§Errors
May return an error if:
- This is a
crate::DynValue
, and the value is not actually a tensor. (for typedTensor
s, use the infallibleTensor::extract_tensor_mut
instead) - The provided type
T
does not match the tensor’s element type.
sourcepub fn try_extract_raw_tensor<T: IntoTensorElementType>(
&self
) -> Result<(Vec<i64>, &[T])>
pub fn try_extract_raw_tensor<T: IntoTensorElementType>( &self ) -> Result<(Vec<i64>, &[T])>
Attempt to extract the underlying data into a “raw” view tuple, consisting of the tensor’s dimensions and an immutable view into its data.
See also:
- the mutable counterpart of this function,
Tensor::try_extract_raw_tensor_mut
. - the infallible counterpart,
Tensor::extract_raw_tensor
, for typedTensor<T>
s. - the alternative function for strings,
Tensor::try_extract_raw_string_tensor
.
let array = vec![1_i64, 2, 3, 4, 5];
let value = Value::from_array(([array.len()], array.clone().into_boxed_slice()))?;
let (extracted_shape, extracted_data) = value.try_extract_raw_tensor::<i64>()?;
assert_eq!(extracted_data, &array);
assert_eq!(extracted_shape, [5]);
§Errors
May return an error if:
- This is a
crate::DynValue
, and the value is not actually a tensor. (for typedTensor
s, use the infallibleTensor::extract_raw_tensor
instead) - The provided type
T
does not match the tensor’s element type.
sourcepub fn try_extract_raw_tensor_mut<T: IntoTensorElementType>(
&mut self
) -> Result<(Vec<i64>, &mut [T])>
pub fn try_extract_raw_tensor_mut<T: IntoTensorElementType>( &mut self ) -> Result<(Vec<i64>, &mut [T])>
Attempt to extract the underlying data into a “raw” view tuple, consisting of the tensor’s dimensions and a mutable view into its data.
See also the infallible counterpart, Tensor::extract_raw_tensor_mut
, for typed Tensor<T>
s.
let array = vec![1_i64, 2, 3, 4, 5];
let mut value = Value::from_array(([array.len()], array.clone().into_boxed_slice()))?;
let (extracted_shape, extracted_data) = value.try_extract_raw_tensor_mut::<i64>()?;
assert_eq!(extracted_data, &array);
assert_eq!(extracted_shape, [5]);
§Errors
May return an error if:
- This is a
crate::DynValue
, and the value is not actually a tensor. (for typedTensor
s, use the infallibleTensor::extract_raw_tensor_mut
instead) - The provided type
T
does not match the tensor’s element type.
sourcepub fn try_extract_string_tensor(&self) -> Result<ArrayD<String>>
Available on crate feature ndarray
only.
pub fn try_extract_string_tensor(&self) -> Result<ArrayD<String>>
ndarray
only.Attempt to extract the underlying data into a Rust ndarray
.
let array = ndarray::Array1::from_vec(vec!["hello", "world"]);
let tensor = DynTensor::from_string_array(&allocator, array.clone())?;
let extracted = tensor.try_extract_string_tensor()?;
assert_eq!(array.into_dyn(), extracted);
sourcepub fn try_extract_raw_string_tensor(&self) -> Result<(Vec<i64>, Vec<String>)>
pub fn try_extract_raw_string_tensor(&self) -> Result<(Vec<i64>, Vec<String>)>
Attempt to extract the underlying string data into a “raw” data tuple, consisting of the tensor’s dimensions and
an owned Vec
of its data.
let array = vec!["hello", "world"];
let tensor = DynTensor::from_string_array(&allocator, ([array.len()], array.clone().into_boxed_slice()))?;
let (extracted_shape, extracted_data) = tensor.try_extract_raw_string_tensor()?;
assert_eq!(extracted_data, array);
assert_eq!(extracted_shape, [2]);
sourcepub fn shape(&self) -> Result<Vec<i64>>
pub fn shape(&self) -> Result<Vec<i64>>
Returns the shape of the tensor.
let tensor = Tensor::<f32>::new(&allocator, [1, 128, 128, 3])?;
assert_eq!(tensor.shape()?, &[1, 128, 128, 3]);
sourcepub fn extract_tensor(&self) -> ArrayViewD<'_, T>
Available on crate feature ndarray
only.
pub fn extract_tensor(&self) -> ArrayViewD<'_, T>
ndarray
only.Extracts the underlying data into a read-only ndarray::ArrayView
.
let array = ndarray::Array4::<f32>::ones((1, 16, 16, 3));
let tensor = Tensor::from_array(array.view())?;
let extracted = tensor.extract_tensor();
assert_eq!(array.into_dyn(), extracted);
sourcepub fn extract_tensor_mut(&mut self) -> ArrayViewMutD<'_, T>
Available on crate feature ndarray
only.
pub fn extract_tensor_mut(&mut self) -> ArrayViewMutD<'_, T>
ndarray
only.Extracts the underlying data into a mutable ndarray::ArrayViewMut
.
let array = ndarray::Array4::<f32>::ones((1, 16, 16, 3));
let mut tensor = Tensor::from_array(array.view())?;
let mut extracted = tensor.extract_tensor_mut();
extracted[[0, 0, 0, 1]] = 0.0;
let mut array = array.into_dyn();
assert_ne!(array, extracted);
array[[0, 0, 0, 1]] = 0.0;
assert_eq!(array, extracted);
sourcepub fn extract_raw_tensor(&self) -> (Vec<i64>, &[T])
pub fn extract_raw_tensor(&self) -> (Vec<i64>, &[T])
Extracts the underlying data into a “raw” view tuple, consisting of the tensor’s dimensions and an immutable view into its data.
let array = vec![1_i64, 2, 3, 4, 5];
let tensor = Tensor::from_array(([array.len()], array.clone().into_boxed_slice()))?;
let (extracted_shape, extracted_data) = tensor.extract_raw_tensor();
assert_eq!(extracted_data, &array);
assert_eq!(extracted_shape, [5]);
sourcepub fn extract_raw_tensor_mut(&mut self) -> (Vec<i64>, &mut [T])
pub fn extract_raw_tensor_mut(&mut self) -> (Vec<i64>, &mut [T])
Extracts the underlying data into a “raw” view tuple, consisting of the tensor’s dimensions and a mutable view into its data.
let array = vec![1_i64, 2, 3, 4, 5];
let tensor = Tensor::from_array(([array.len()], array.clone().into_boxed_slice()))?;
let (extracted_shape, extracted_data) = tensor.extract_raw_tensor();
assert_eq!(extracted_data, &array);
assert_eq!(extracted_shape, [5]);
sourcepub fn data_ptr_mut(&mut self) -> Result<*mut c_void>
pub fn data_ptr_mut(&mut self) -> Result<*mut c_void>
Returns a mutable pointer to the tensor’s data.
sourcepub fn memory_info(&self) -> Result<MemoryInfo>
pub fn memory_info(&self) -> Result<MemoryInfo>
Returns information about the device this tensor is allocated on.
sourcepub fn upcast_ref(&self) -> DynTensorRef<'_>
pub fn upcast_ref(&self) -> DynTensorRef<'_>
sourcepub fn upcast_mut(&mut self) -> DynTensorRefMut<'_>
pub fn upcast_mut(&mut self) -> DynTensorRefMut<'_>
sourcepub fn ptr(&self) -> *mut OrtValue
pub fn ptr(&self) -> *mut OrtValue
Returns the underlying ort_sys::OrtValue
pointer.
sourcepub fn view_mut(&mut self) -> ValueRefMut<'_, Type>
pub fn view_mut(&mut self) -> ValueRefMut<'_, Type>
Create a mutable view of this value’s data.
sourcepub fn is_tensor(&self) -> Result<bool>
pub fn is_tensor(&self) -> Result<bool>
Returns true
if this value is a tensor, or false
if it is another type (sequence, map).
// Create a tensor from a raw data vector
let tensor_value = Value::from_array(([3usize], vec![1.0_f32, 2.0, 3.0].into_boxed_slice()))?;
assert!(tensor_value.is_tensor()?);
sourcepub fn downcast_ref<OtherType: ValueTypeMarker + DowncastableTarget + Debug + ?Sized>(
&self
) -> Result<ValueRef<'_, OtherType>>
pub fn downcast_ref<OtherType: ValueTypeMarker + DowncastableTarget + Debug + ?Sized>( &self ) -> Result<ValueRef<'_, OtherType>>
Attempts to downcast a dynamic value (like DynValue
or DynTensor
) to a more strongly typed reference
variant, like TensorRef<T>
.
sourcepub fn downcast_mut<OtherType: ValueTypeMarker + DowncastableTarget + Debug + ?Sized>(
&mut self
) -> Result<ValueRefMut<'_, OtherType>>
pub fn downcast_mut<OtherType: ValueTypeMarker + DowncastableTarget + Debug + ?Sized>( &mut self ) -> Result<ValueRefMut<'_, OtherType>>
Attempts to upcast a dynamic value (like DynValue
or DynTensor
) to a more strongly typed
mutable-reference variant, like TensorRefMut<T>
.