Skip to main content

detach

Function detach 

Source
pub fn detach(
    eager: &EagerTensor,
    engine: &mut dyn Engine,
) -> Result<Tensor, ConversionError>
Expand description

Materialize a tracked eager value into a plain concrete value on engine.

This is the explicit AD to plain boundary of the coexistence contract: the caller names the conversion, so tracking never disappears silently, and no_grad on an eager value is not this operation and does not select the concrete representation. The value keeps its dtype, logical shape and column-major layout; the result is an ordinary concrete tensor that this frontend’s routes accept. Materializing a lazy or prepared value needs an engine to evaluate it, so the caller passes the one it wants the copy to run on.

§Errors

Returns ConversionError::Detach when the tracked value cannot be materialized, for example because it is a lazy or prepared value that has not been evaluated.

§Examples

use tensor4all_tensorbackend::{explicit, CpuExecutionContext};
use tenferro::Tensor;
use tenferro_ad::EagerTensor;
use tensor4all_tensorbackend::CpuBackend;

let context = CpuExecutionContext::from_backend(CpuBackend::with_threads(1)?);
let plain = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let tracked = explicit::lift(&context, plain)?;
let back = context.with_backend(|backend| explicit::detach(&tracked, backend.engine_mut()))?;
assert_eq!(back.as_slice::<f64>()?, &[1.0, 2.0]);