Struct MemoryEfficientLayer

Source
pub struct MemoryEfficientLayer { /* private fields */ }
Expand description

Memory-efficient neural network layer that processes data in chunks

Implementations§

Source§

impl MemoryEfficientLayer

Source

pub fn new( input_size: usize, output_size: usize, chunk_size: Option<usize>, ) -> Result<Self>

Create a new memory-efficient layer

Examples found in repository?
examples/memory_efficient_example.rs (lines 228-232)
223fn demo_memory_efficient_layer() -> Result<()> {
224    println!("\n🧠 Memory-Efficient Layer Demo");
225    println!("------------------------------");
226
227    // Create a memory-efficient layer
228    let layer = MemoryEfficientLayer::new(
229        784,      // Input size (e.g., 28x28 MNIST)
230        128,      // Output size
231        Some(64), // Chunk size
232    )?;
233
234    println!("Created memory-efficient layer: 784 -> 128");
235
236    // Create input data
237    let input =
238        Array2::from_shape_fn((256, 784), |(i, j)| ((i + j) as f32 * 0.01).sin()).into_dyn();
239
240    println!("Input shape: {:?}", input.shape());
241
242    // Forward pass
243    println!("Performing forward pass...");
244    let start = Instant::now();
245    let output = layer.forward(&input)?;
246    let forward_time = start.elapsed();
247
248    println!("Forward pass completed in {:?}", forward_time);
249    println!("Output shape: {:?}", output.shape());
250
251    // Verify output statistics
252    let mean = output.mean().unwrap_or(0.0);
253    let std = {
254        let variance = output.mapv(|x| (x - mean).powi(2)).mean().unwrap_or(0.0);
255        variance.sqrt()
256    };
257
258    println!("Output statistics:");
259    println!("  Mean: {:.6}", mean);
260    println!("  Std: {:.6}", std);
261    println!(
262        "  Min: {:.6}",
263        output.iter().cloned().fold(f32::INFINITY, f32::min)
264    );
265    println!(
266        "  Max: {:.6}",
267        output.iter().cloned().fold(f32::NEG_INFINITY, f32::max)
268    );
269
270    Ok(())
271}
Source

pub fn forward(&self, input: &ArrayD<f32>) -> Result<ArrayD<f32>>

Forward pass with memory-efficient chunk processing

Examples found in repository?
examples/memory_efficient_example.rs (line 245)
223fn demo_memory_efficient_layer() -> Result<()> {
224    println!("\n🧠 Memory-Efficient Layer Demo");
225    println!("------------------------------");
226
227    // Create a memory-efficient layer
228    let layer = MemoryEfficientLayer::new(
229        784,      // Input size (e.g., 28x28 MNIST)
230        128,      // Output size
231        Some(64), // Chunk size
232    )?;
233
234    println!("Created memory-efficient layer: 784 -> 128");
235
236    // Create input data
237    let input =
238        Array2::from_shape_fn((256, 784), |(i, j)| ((i + j) as f32 * 0.01).sin()).into_dyn();
239
240    println!("Input shape: {:?}", input.shape());
241
242    // Forward pass
243    println!("Performing forward pass...");
244    let start = Instant::now();
245    let output = layer.forward(&input)?;
246    let forward_time = start.elapsed();
247
248    println!("Forward pass completed in {:?}", forward_time);
249    println!("Output shape: {:?}", output.shape());
250
251    // Verify output statistics
252    let mean = output.mean().unwrap_or(0.0);
253    let std = {
254        let variance = output.mapv(|x| (x - mean).powi(2)).mean().unwrap_or(0.0);
255        variance.sqrt()
256    };
257
258    println!("Output statistics:");
259    println!("  Mean: {:.6}", mean);
260    println!("  Std: {:.6}", std);
261    println!(
262        "  Min: {:.6}",
263        output.iter().cloned().fold(f32::INFINITY, f32::min)
264    );
265    println!(
266        "  Max: {:.6}",
267        output.iter().cloned().fold(f32::NEG_INFINITY, f32::max)
268    );
269
270    Ok(())
271}

Auto Trait Implementations§

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> 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