Skip to main content

GpuTensor

Struct GpuTensor 

Source
pub struct GpuTensor<T: GpuFloat> { /* private fields */ }
Expand description

A tensor residing on a CUDA GPU.

Wraps a CudaBuffer<T> with shape metadata and a reference to the GpuDevice that owns the memory. Created by tensor_to_gpu or the convenience functions cuda / cuda_default.

Convert back to a CPU Tensor with GpuTensor::cpu or the free function tensor_to_cpu.

Implementations§

Source§

impl<T: GpuFloat> GpuTensor<T>

Source

pub fn shape(&self) -> &[usize]

The shape of this tensor.

Source

pub fn numel(&self) -> usize

Total number of elements.

Source

pub fn device(&self) -> &GpuDevice

The GPU device that holds this tensor’s data.

Source

pub fn buffer(&self) -> &CudaBuffer<T>

Borrow the underlying CudaBuffer.

Source

pub fn ndim(&self) -> usize

Number of dimensions.

Source

pub fn cpu(&self) -> FerrotorchResult<Tensor<T>>

Copy this tensor back to CPU, returning a Tensor<T>.

This is a convenience wrapper around tensor_to_cpu.

Source§

impl<T: GpuFloat> GpuTensor<T>

Source

pub fn add(&self, other: &GpuTensor<T>) -> GpuResult<GpuTensor<T>>

Elementwise addition: out[i] = self[i] + other[i].

Uses a PTX kernel for f32; falls back to CPU round-trip for f64.

§Errors
Source

pub fn sub(&self, other: &GpuTensor<T>) -> GpuResult<GpuTensor<T>>

Elementwise subtraction: out[i] = self[i] - other[i].

Uses a PTX kernel for f32; falls back to CPU round-trip for f64.

Source

pub fn mul(&self, other: &GpuTensor<T>) -> GpuResult<GpuTensor<T>>

Elementwise multiplication: out[i] = self[i] * other[i].

Uses a PTX kernel for f32; falls back to CPU round-trip for f64.

Source

pub fn neg(&self) -> GpuResult<GpuTensor<T>>

Elementwise negation: out[i] = -self[i].

Uses a PTX kernel for f32; falls back to CPU round-trip for f64.

Source

pub fn relu(&self) -> GpuResult<GpuTensor<T>>

Elementwise ReLU: out[i] = max(self[i], 0).

Uses a PTX kernel for f32; falls back to CPU round-trip for f64.

Source

pub fn matmul(&self, other: &GpuTensor<T>) -> GpuResult<GpuTensor<T>>

Matrix multiplication: C = self @ other.

Both tensors must be 2-D. self has shape [m, k] and other has shape [k, n]. The result has shape [m, n].

Uses cuBLAS SGEMM for f32 and DGEMM for f64.

§Errors
Source

pub fn conv2d( &self, weight: &GpuTensor<T>, bias: Option<&GpuTensor<T>>, stride: (usize, usize), padding: (usize, usize), ) -> GpuResult<GpuTensor<T>>

2-D convolution: output = conv2d(self, weight, bias).

Uses im2col (CPU) + cuBLAS GEMM (GPU) — no cuDNN required.

self must have shape [B, C_in, H, W] and weight must have shape [C_out, C_in, kH, kW]. bias, if provided, must have shape [C_out]. The result has shape [B, C_out, H_out, W_out].

Currently only supports f32. For f64 tensors, returns GpuError::ShapeMismatch (f64 conv path not yet implemented).

§Errors

Trait Implementations§

Source§

impl<T: GpuFloat> Debug for GpuTensor<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for GpuTensor<T>

§

impl<T> RefUnwindSafe for GpuTensor<T>
where T: RefUnwindSafe,

§

impl<T> Send for GpuTensor<T>

§

impl<T> Sync for GpuTensor<T>

§

impl<T> Unpin for GpuTensor<T>

§

impl<T> UnsafeUnpin for GpuTensor<T>

§

impl<T> UnwindSafe for GpuTensor<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<T> DistributionExt for T
where T: ?Sized,

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,