pub struct TensorHandle<'a> { /* private fields */ }
Expand description

A handle to a tensor on a device.

Constructing a TensorHandle requires a reference to an execute context so that the generated handle will not out live the context.

use tensorflow::eager::*;
let opts = ContextOptions::new();
let ctx = Context::new(opts)?;

let t = Tensor::from(&[3i32]).freeze();
let h = TensorHandle::new(&ctx, &t)?;
let v = h.resolve::<i32>()?;
assert_eq!(&v[..], &[3i32]);

TensorHandle manages the same buffer of the tensor. Users can destruct the Tensor while leaving the TensorHandle. This is a valid use case for TensorHandle.

use tensorflow::eager::*;

let opts = ContextOptions::new();
let ctx = Context::new(opts)?;
let h = {
    let t = Tensor::from(&[3i32]).freeze();
    TensorHandle::new(&ctx, &t)?
};
// At this point, the buffer is managed only by the handle.

Since TensorHandle cannot be alive beyond the lifetime of the context, the following code will not compile.

use tensorflow::eager::*;

let h = {
    let opts = ContextOptions::new();
    let ctx = Context::new(opts)?;

    let t = Tensor::from(&[3i32]).freeze();
    TensorHandle::new(&ctx, &t)?
};

Implementations§

source§

impl<'a> TensorHandle<'a>

source

pub fn new<T: TensorType>( _ctx: &'a Context, t: &ReadonlyTensor<T> ) -> Result<TensorHandle<'a>>

Create a TensorHandle from the input Tensor

source

pub fn data_type(&self) -> DataType

Return the DataType that corresponds to this type.

source

pub fn num_dims(&self) -> Result<usize>

Return the number of dimensions.

This function will block till the operation that produces the TensorHandle has completed.

source

pub fn num_elements(&self) -> Result<u64>

Return the number of elements

source

pub fn dim(&self, dim_index: i32) -> Result<u64>

Return the number of elements for a given dim_index.

This function will block till the operation that produces the TensorHandle has completed.

source

pub fn device_name(&self) -> Result<String>

Return the device of the operation that produced the current TensorHandle.

If the TensorHandle was produced by a copy, returns the destination device of the copy. Note that the returned device name is not always the device holding the tensor handle’s memory. If you want the latter, use backing_device_name.

This function will block till the operation that produces the current TensorHandle has completed.

source

pub fn backing_device_name(&self) -> Result<String>

Returns the name of the device in whose memory underlying the current TensorHandle resides.

This function will block till the operation that produces the current TensorHandle has completed.

source

pub fn copy_sharing_tensor(&self) -> Result<Self>

Return a new TensorHandle that shares the underlying tensor with the current TensorHandle.

source

pub fn resolve<T: TensorType>(&self) -> Result<ReadonlyTensor<T>>

This function will block till the operation that produces the current TensorHandle has completed. The memory returned might alias the internal memory used by TensorFlow. Hence, callers should not mutate this memory.

source

pub fn copy_to_device<'b>( &self, ctx: &'b Context, device_name: &str ) -> Result<TensorHandle<'b>>

Create a new TensorHandle with the same contents as the current TensorHandle but placed in the memory of the device name ‘device_name’. If source and destination are the same device, then this creates a new handle that shares the underlying buffer. Otherwise, it currently requires at least one of the source or destination devices to be CPU (i.e., for the source or destination tensor to be placed in host memory). If async execution is enabled, the copy may be enqueued and the call will return “non-ready” TensorHandle. Else, this function returns after the copy has been done.

Trait Implementations§

source§

impl<'a> Debug for TensorHandle<'a>

source§

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

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

impl<'a> Drop for TensorHandle<'a>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a> ToTensorHandle<'a> for TensorHandle<'a>

source§

fn to_handle(&self, _: &'a Context) -> Result<TensorHandle<'a>>

Convert a Tensor or values into a new TensorHandle.

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for TensorHandle<'a>

§

impl<'a> !Send for TensorHandle<'a>

§

impl<'a> !Sync for TensorHandle<'a>

§

impl<'a> Unpin for TensorHandle<'a>

§

impl<'a> UnwindSafe for TensorHandle<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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 Twhere 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 Twhere 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.