Struct tensorflow::eager::TensorHandle
source · 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>
impl<'a> TensorHandle<'a>
sourcepub fn new<T: TensorType>(
_ctx: &'a Context,
t: &ReadonlyTensor<T>
) -> Result<TensorHandle<'a>>
pub fn new<T: TensorType>( _ctx: &'a Context, t: &ReadonlyTensor<T> ) -> Result<TensorHandle<'a>>
Create a TensorHandle from the input Tensor
sourcepub fn num_dims(&self) -> Result<usize>
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.
sourcepub fn num_elements(&self) -> Result<u64>
pub fn num_elements(&self) -> Result<u64>
Return the number of elements
sourcepub fn dim(&self, dim_index: i32) -> Result<u64>
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.
sourcepub fn device_name(&self) -> Result<String>
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.
sourcepub fn backing_device_name(&self) -> Result<String>
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.
sourcepub fn copy_sharing_tensor(&self) -> Result<Self>
pub fn copy_sharing_tensor(&self) -> Result<Self>
Return a new TensorHandle that shares the underlying tensor with the current TensorHandle.
sourcepub fn resolve<T: TensorType>(&self) -> Result<ReadonlyTensor<T>>
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.
sourcepub fn copy_to_device<'b>(
&self,
ctx: &'b Context,
device_name: &str
) -> Result<TensorHandle<'b>>
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.