Skip to main content

SafeVariableEnvironment

Struct SafeVariableEnvironment 

Source
pub struct SafeVariableEnvironment<F: Float + Send + Sync> { /* private fields */ }
Expand description

Thread-safe alternative to VariableEnvironment for global usage and PyTorch-compatible APIs

This wrapper solves the thread safety issues with RefCell-based VariableEnvironment, enabling global autograd environments and multi-threaded gradient computation.

Key Features:

  • Thread-safe: Uses Arc<RwLock<>> for shared ownership and concurrent access
  • Global-safe: Can be used in static variables and lazy_static
  • PyTorch-compatible: Provides backward() API for autograd integration
  • Performance: Optimized for multi-threaded gradient computation

Usage Example:

use scirs2_autograd::SafeVariableEnvironment;
use std::sync::Arc;

// Thread-safe operations
let env = SafeVariableEnvironment::new();
let arr = scirs2_core::ndarray::arr2(&[[1.0, 2.0], [3.0, 4.0]]).into_dyn();
let var_id = env.set_variable(arr).expect("Operation failed");
env.backward(var_id).expect("Operation failed");

Implementations§

Source§

impl<F: Float + Send + Sync> SafeVariableEnvironment<F>

Source

pub fn new() -> Self

Creates a new thread-safe variable environment

Source

pub fn set_variable( &self, array: NdArray<F>, ) -> Result<VariableID, Box<dyn Error + Send + Sync>>

Sets a variable array and returns its ID (thread-safe)

Source

pub fn name_variable<S: AsRef<str>>( &self, name: S, array: NdArray<F>, ) -> Result<VariableID, Box<dyn Error + Send + Sync>>

Names a variable for later lookup (thread-safe)

Source

pub fn get_variable( &self, var_id: VariableID, ) -> Result<NdArray<F>, Box<dyn Error + Send + Sync>>

Gets a copy of a variable array (thread-safe)

Source

pub fn backward( &self, output_var: VariableID, ) -> Result<(), Box<dyn Error + Send + Sync>>

PyTorch-compatible backward pass implementation

This provides the backward() API that ToRSh expects for autograd integration. Unlike the graph-based execution model, this provides direct tensor-level backward passes.

Source

pub fn parallel_backward_pass( &self, outputs: &[VariableID], _inputs: &[VariableID], ) -> Result<Vec<Option<NdArray<F>>>, Box<dyn Error + Send + Sync>>

High-performance parallel gradient computation

This addresses ToRSh’s requirement for parallel backward pass implementation targeting 10-50x speedup for gradient computation.

Source

pub fn run<R>( &self, func: impl FnOnce(&VariableEnvironment<F>) -> R, ) -> Result<R, Box<dyn Error + Send + Sync>>

Execute operations within the environment context (thread-safe)

Source

pub fn len(&self) -> Result<usize, Box<dyn Error + Send + Sync>>

Get the number of variables in the environment (thread-safe)

Source

pub fn is_empty(&self) -> Result<bool, Box<dyn Error + Send + Sync>>

Check if the environment is empty (thread-safe)

Trait Implementations§

Source§

impl<F: Clone + Float + Send + Sync> Clone for SafeVariableEnvironment<F>

Source§

fn clone(&self) -> SafeVariableEnvironment<F>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Float + Send + Sync> Default for SafeVariableEnvironment<F>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<F: Float + Send + Sync> Send for SafeVariableEnvironment<F>

Implement Send + Sync for thread safety

Source§

impl<F: Float + Send + Sync> Sync for SafeVariableEnvironment<F>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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