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>>

source

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>

Methods from Deref<Target = Value<Type>>§

source

pub fn try_extract_map<K: IntoTensorElementType + Clone + Hash + Eq, V: IntoTensorElementType + Clone>( &self, allocator: &Allocator ) -> Result<HashMap<K, V>>

source

pub fn extract_map(&self, allocator: &Allocator) -> HashMap<K, V>

source

pub fn upcast_ref(&self) -> DynMapRef<'_>

Converts from a strongly-typed Map<K, V> to a reference to a type-erased DynMap.

source

pub fn upcast_mut(&mut self) -> DynMapRefMut<'_>

Converts from a strongly-typed Map<K, V> to a mutable reference to a type-erased DynMap.

source

pub fn try_extract_sequence<'s, OtherType: ValueTypeMarker + DowncastableTarget + Debug + Sized>( &'s self, allocator: &Allocator ) -> Result<Vec<ValueRef<'s, OtherType>>>

source

pub fn extract_sequence<'s>( &'s self, allocator: &Allocator ) -> Vec<ValueRef<'s, T>>

source

pub fn upcast_ref(&self) -> DynSequenceRef<'_>

Converts from a strongly-typed Sequence<T> to a reference to a type-erased DynSequence.

source

pub fn upcast_mut(&mut self) -> DynSequenceRefMut<'_>

Converts from a strongly-typed Sequence<T> to a mutable reference to a type-erased DynSequence.

source

pub fn try_extract_tensor<T: IntoTensorElementType>( &self ) -> Result<ArrayViewD<'_, T>>

Available on crate feature ndarray only.

Attempt to extract the underlying data of type T into a read-only ndarray::ArrayView.

See also:

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:

source

pub fn try_extract_tensor_mut<T: IntoTensorElementType>( &mut self ) -> Result<ArrayViewMutD<'_, T>>

Available on crate feature 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:

source

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:

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:

source

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:

source

pub fn try_extract_string_tensor(&self) -> Result<ArrayD<String>>

Available on crate feature 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);
source

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]);
source

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]);
source

pub fn extract_tensor(&self) -> ArrayViewD<'_, T>

Available on crate feature 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);
source

pub fn extract_tensor_mut(&mut self) -> ArrayViewMutD<'_, T>

Available on crate feature 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);
source

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]);
source

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]);
source

pub fn data_ptr_mut(&mut self) -> Result<*mut c_void>

Returns a mutable pointer to the tensor’s data.

source

pub fn data_ptr(&self) -> Result<*const c_void>

Returns a pointer to the tensor’s data.

source

pub fn memory_info(&self) -> Result<MemoryInfo>

Returns information about the device this tensor is allocated on.

source

pub fn upcast_ref(&self) -> DynTensorRef<'_>

Converts from a strongly-typed Tensor<T> to a reference to a type-erased DynTensor.

source

pub fn upcast_mut(&mut self) -> DynTensorRefMut<'_>

Converts from a strongly-typed Tensor<T> to a mutable reference to a type-erased DynTensor.

source

pub fn dtype(&self) -> Result<ValueType>

Returns the data type of this Value.

source

pub fn ptr(&self) -> *mut OrtValue

Returns the underlying ort_sys::OrtValue pointer.

source

pub fn view(&self) -> ValueRef<'_, Type>

Create a view of this value’s data.

source

pub fn view_mut(&mut self) -> ValueRefMut<'_, Type>

Create a mutable view of this value’s data.

source

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()?);
source

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>.

source

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>.

Trait Implementations§

source§

impl<'v, Type: Debug + ValueTypeMarker + ?Sized> Debug for ValueRefMut<'v, Type>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'v, Type: ValueTypeMarker + ?Sized> Deref for ValueRefMut<'v, Type>

§

type Target = Value<Type>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<'v, Type: ValueTypeMarker + ?Sized> DerefMut for ValueRefMut<'v, Type>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<'v, T: ValueTypeMarker + ?Sized> From<ValueRefMut<'v, T>> for SessionInputValue<'v>

source§

fn from(value: ValueRefMut<'v, T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'v, Type> Freeze for ValueRefMut<'v, Type>
where Type: ?Sized,

§

impl<'v, Type = DynValueTypeMarker> !RefUnwindSafe for ValueRefMut<'v, Type>

§

impl<'v, Type = DynValueTypeMarker> !Send for ValueRefMut<'v, Type>

§

impl<'v, Type = DynValueTypeMarker> !Sync for ValueRefMut<'v, Type>

§

impl<'v, Type> Unpin for ValueRefMut<'v, Type>
where Type: Unpin + ?Sized,

§

impl<'v, Type = DynValueTypeMarker> !UnwindSafe for ValueRefMut<'v, Type>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more