Skip to main content

safe_tensor_op

Function safe_tensor_op 

Source
pub fn safe_tensor_op<F, T>(operation: F) -> Result<T, Error>
where F: FnOnce() -> T + UnwindSafe,
Expand description

Safely execute a tensor operation that might panic (e.g., OOM)

PyTorch operations like Tensor::zeros() and to_device() can panic when GPU memory is exhausted. This wrapper catches such panics and converts them to proper Result errors.

ยงExample

use tch::{Device, Tensor};
use hyprstream_inference::runtime::torch_utils::safe_tensor_op;
let result = safe_tensor_op(|| {
    Tensor::zeros([1024, 1024], (tch::Kind::BFloat16, Device::Cuda(0)))
});