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>
impl<F: Float + Send + Sync> SafeVariableEnvironment<F>
Sourcepub fn set_variable(
&self,
array: NdArray<F>,
) -> Result<VariableID, Box<dyn Error + Send + Sync>>
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)
Sourcepub fn name_variable<S: AsRef<str>>(
&self,
name: S,
array: NdArray<F>,
) -> Result<VariableID, Box<dyn Error + Send + Sync>>
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)
Sourcepub fn get_variable(
&self,
var_id: VariableID,
) -> Result<NdArray<F>, Box<dyn Error + Send + Sync>>
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)
Sourcepub fn backward(
&self,
output_var: VariableID,
) -> Result<(), Box<dyn Error + Send + Sync>>
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.
Sourcepub fn parallel_backward_pass(
&self,
outputs: &[VariableID],
_inputs: &[VariableID],
) -> Result<Vec<Option<NdArray<F>>>, Box<dyn Error + Send + Sync>>
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.
Sourcepub fn run<R>(
&self,
func: impl FnOnce(&VariableEnvironment<F>) -> R,
) -> Result<R, Box<dyn Error + Send + Sync>>
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)
Trait Implementations§
Source§impl<F: Clone + Float + Send + Sync> Clone for SafeVariableEnvironment<F>
impl<F: Clone + Float + Send + Sync> Clone for SafeVariableEnvironment<F>
Source§fn clone(&self) -> SafeVariableEnvironment<F>
fn clone(&self) -> SafeVariableEnvironment<F>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<F: Float + Send + Sync> Send for SafeVariableEnvironment<F>
Implement Send + Sync for thread safety
impl<F: Float + Send + Sync> Sync for SafeVariableEnvironment<F>
Auto Trait Implementations§
impl<F> Freeze for SafeVariableEnvironment<F>
impl<F> RefUnwindSafe for SafeVariableEnvironment<F>
impl<F> Unpin for SafeVariableEnvironment<F>
impl<F> UnwindSafe for SafeVariableEnvironment<F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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