Hpt
Home
GitHub
crate.io
Benchmarks
Home
GitHub
crate.io
Benchmarks
  • Docs

    • unary

      • sin
      • sin_
      • cos
      • cos_
      • tan
      • tan_
      • sinh
      • sinh_
      • cosh
      • cosh_
      • tanh
      • tanh_
      • asinh
      • asinh_
      • acosh
      • acosh_
      • atanh
      • atanh_
      • asin
      • asin_
      • acos
      • acos_
      • atan
      • atan_
      • exp
      • exp_
      • exp2
      • exp2_
      • sqrt
      • sqrt_
      • recip
      • recip_
      • ln
      • ln_
      • log2
      • log2_
      • log10
      • log10_
      • celu
      • celu_
      • sigmoid
      • sigmoid_
      • elu
      • elu_
      • erf
      • gelu
      • gelu_
      • selu
      • selu_
      • hard_sigmoid
      • hard_sigmoid_
      • hard_swish
      • hard_swish_
      • softplus
      • softplus_
      • softsign
      • softsign_
      • mish
      • mish_
      • cbrt
      • cbrt_
      • sincos
      • sincos_
      • exp10
      • exp10_
    • binary

      • add
      • add_
      • sub
      • sub_
      • mul
      • mul_
      • div
      • div_
      • rem
      • rem_
      • pow
      • pow_
      • hypot
      • hypot_
    • reduce

      • logsumexp
      • argmin
      • argmax
      • max
      • min
      • mean
      • sum
      • sum_
      • nansum
      • nansum_
      • prod
      • nanprod
      • sum_square
      • reducel1
      • reducel2
      • reducel3
      • all
      • any
    • conv

      • cuda

        • conv2d_group
        • conv2d
        • dwconv2d
        • batchnorm_conv2d
      • cpu

        • batchnorm_conv2d
        • conv2d_group
        • conv2d_transpose
        • conv2d
        • dwconv2d
    • pooling

      • maxpool2d
      • avgpool2d
      • adaptive_maxpool2d
      • adaptive_avgpool2d
    • compare

      • tensor_neq
      • tensor_eq
      • tensor_gt
      • tensor_lt
      • tensor_ge
      • tensor_le
    • advanced

      • scatter
      • hardmax
      • tensor_where
      • topk
      • onehot
    • normalization

      • log_softmax
      • layernorm
      • softmax
    • cumulative

      • cumsum
      • cumprod
    • regularization

      • dropout
      • shrinkage
    • linalg

      • matmul
      • matmul_post
      • gemm
      • tensordot
    • random

      • randn
      • randn_like
      • rand
      • rand_like
      • beta
      • beta_like
      • chisquare
      • chisquare_like
      • exponential
      • exponential_like
      • gamma
      • gamma_like
      • gumbel
      • gumbel_like
      • lognormal
      • lognormal_like
      • normal_gaussian
      • normal_gaussian_like
      • pareto
      • pareto_like
      • poisson
      • poisson_like
      • weibull
      • weibull_like
      • zipf
      • zipf_like
      • triangular
      • triangular_like
      • bernoulli
      • randint
      • randint_like
    • shape manipulate

      • squeeze
      • unsqueeze
      • reshape
      • transpose
      • permute
      • permute_inv
      • expand
      • t
      • mt
      • flip
      • fliplr
      • flipud
      • tile
      • trim_zeros
      • repeat
      • split
      • dsplit
      • hsplit
      • vsplit
      • swap_axes
      • flatten
      • concat
      • vstack
      • hstack
      • dstack
    • creation

      • empty
      • zeros
      • ones
      • empty_like
      • zeros_like
      • ones_like
      • full
      • full_like
      • arange
      • arange_step
      • eye
      • linspace
      • logspace
      • geomspace
      • tri
      • tril
      • triu
      • identity
    • windows

      • hamming_window
      • hann_window
      • blackman_window
    • iterator

      • par_iter
      • par_iter_mut
      • par_iter_simd
      • par_iter_simd_mut
      • strided_map
      • strided_map_simd
      • collect
    • utils

      • set_display_elements
      • resize_cpu_lru_cache
      • resize_cuda_lru_cache
      • set_seed
      • num_threads
    • associated methods

      • cpu

        • forget
        • forget_copy
        • from_raw
        • to_cuda
      • cuda

        • forget
        • forget_copy
        • from_raw
        • to_cpu
      • all_close
      • astype
    • custom type
    • custom allocator
    • slice
    • save/load

from_raw

unsafe fn from_raw<S: Into<Shape>>(data: *mut T, shape: S) -> Result<Tensor<T>, TensorError>

Creates a new tensor from an existing raw pointer and shape without taking ownership of the memory.

Note

This method allows you to create a tensor that uses an existing memory allocation. The tensor will use the provided memory without allocating new memory or taking ownership of it. This is useful for integrating with external libraries or when you want to manually manage memory.

Parameters:

data: A *mut T pointing to the pre-allocated memory on the CPU device

shape: The shape to use for the tensor, which can be any type that can be converted to Shape

Returns:

Tensor with type T

Examples:

use hpt::{common::Shape, error::TensorError, Tensor};

fn main() -> Result<(), TensorError> {
    let origin_layout = std::alloc::Layout::from_size_align(6 * std::mem::size_of::<f32>(), 64).unwrap();
    let data = unsafe { std::alloc::alloc(origin_layout) };

    // Create a 2x3 tensor using the existing memory
    let shape = Shape::new([2, 3]);
    let tensor = unsafe { Tensor::<f32>::from_raw(data as *mut f32, shape)? };

    println!("{}", tensor); // Should print the 2x3 tensor with the values

    // Note: When the tensor is dropped, it will NOT deallocate the memory
    // since it did not take ownership of it

    // it is better to use forget method to transfer the ownership of the memory to the caller
    // the forget method will track the reference count of the memory
    let (raw_ptr, layout) = unsafe { tensor.forget() }?;
    assert_eq!(origin_layout, layout);
    unsafe { std::alloc::dealloc(raw_ptr, layout) };
    Ok(())
}
最近更新: 2025/6/24 21:23
Contributors: Jianqoq
Prev
forget_copy
Next
to_cuda