pub struct DynamicGraph { /* private fields */ }Expand description
Dynamic computation graph that can modify its structure at runtime
This container provides a flexible execution framework for neural networks that need complex control flow, conditional execution, or runtime adaptation. It supports modules, conditions, combiners, and custom functions that can be composed into arbitrary computational graphs.
§Examples
let mut graph = DynamicGraph::new();
graph.add_module("linear1".to_string(), Linear::new(784, 128, true));
graph.add_module("linear2".to_string(), Linear::new(128, 10, true));
// Create a simple sequential execution graph
let seq_graph = DynamicGraph::sequential(vec![
"linear1".to_string(),
"linear2".to_string(),
]);
graph.set_graph(seq_graph);
// Create input and run forward pass
let input = randn(&[1, 784])?;
let output = graph.forward(&input)?;Implementations§
Source§impl DynamicGraph
impl DynamicGraph
Sourcepub fn add_module<M: Module + 'static>(&mut self, name: String, module: M)
pub fn add_module<M: Module + 'static>(&mut self, name: String, module: M)
Add a module to the graph
Sourcepub fn add_condition<F>(&mut self, name: String, condition: F)
pub fn add_condition<F>(&mut self, name: String, condition: F)
Add a condition function
Sourcepub fn add_combiner<F>(&mut self, name: String, combiner: F)
pub fn add_combiner<F>(&mut self, name: String, combiner: F)
Add a combiner function for parallel execution
Sourcepub fn add_function<F>(&mut self, name: String, function: F)
pub fn add_function<F>(&mut self, name: String, function: F)
Add a custom function
Sourcepub fn sequential(module_names: Vec<String>) -> GraphNode
pub fn sequential(module_names: Vec<String>) -> GraphNode
Create a sequential graph from module names
Sourcepub fn conditional(
condition: String,
true_branch: GraphNode,
false_branch: Option<GraphNode>,
) -> GraphNode
pub fn conditional( condition: String, true_branch: GraphNode, false_branch: Option<GraphNode>, ) -> GraphNode
Create a conditional graph
Sourcepub fn loop_graph(
body: GraphNode,
condition: String,
max_iterations: usize,
) -> GraphNode
pub fn loop_graph( body: GraphNode, condition: String, max_iterations: usize, ) -> GraphNode
Create a loop graph
Sourcepub fn get_execution_history(&self) -> Vec<String>
pub fn get_execution_history(&self) -> Vec<String>
Get execution history for debugging
Sourcepub fn clear_execution_history(&self)
pub fn clear_execution_history(&self)
Clear execution history
Sourcepub fn modify_graph<F>(&mut self, modifier: F)
pub fn modify_graph<F>(&mut self, modifier: F)
Dynamically modify the graph at runtime
Sourcepub fn get_module(&self, name: &str) -> Option<&dyn Module>
pub fn get_module(&self, name: &str) -> Option<&dyn Module>
Get a reference to a specific module
Sourcepub fn replace_module<M: Module + 'static>(&mut self, name: String, module: M)
pub fn replace_module<M: Module + 'static>(&mut self, name: String, module: M)
Replace a module at runtime
Sourcepub fn module_count(&self) -> usize
pub fn module_count(&self) -> usize
Get the number of modules
Sourcepub fn module_names(&self) -> Vec<&String>
pub fn module_names(&self) -> Vec<&String>
List all module names
Sourcepub fn condition_names(&self) -> Vec<&String>
pub fn condition_names(&self) -> Vec<&String>
List all condition names
Sourcepub fn combiner_names(&self) -> Vec<&String>
pub fn combiner_names(&self) -> Vec<&String>
List all combiner names
Sourcepub fn function_names(&self) -> Vec<&String>
pub fn function_names(&self) -> Vec<&String>
List all function names
Trait Implementations§
Source§impl Default for DynamicGraph
impl Default for DynamicGraph
Source§impl Module for DynamicGraph
impl Module for DynamicGraph
Source§fn parameters(&self) -> HashMap<String, Parameter>
fn parameters(&self) -> HashMap<String, Parameter>
Source§fn named_parameters(&self) -> HashMap<String, Parameter>
fn named_parameters(&self) -> HashMap<String, Parameter>
Source§fn set_training(&mut self, training: bool)
fn set_training(&mut self, training: bool)
Source§fn all_parameters(&self) -> HashMap<String, Parameter>
fn all_parameters(&self) -> HashMap<String, Parameter>
Source§fn all_named_parameters(&self) -> HashMap<String, Parameter>
fn all_named_parameters(&self) -> HashMap<String, Parameter>
Source§fn all_named_buffers(&self) -> HashMap<String, Arc<RwLock<Tensor>>>
fn all_named_buffers(&self) -> HashMap<String, Arc<RwLock<Tensor>>>
Source§fn load_state_dict(
&mut self,
state_dict: &HashMap<String, Tensor>,
strict: bool,
) -> Result<()>
fn load_state_dict( &mut self, state_dict: &HashMap<String, Tensor>, strict: bool, ) -> Result<()>
Source§fn load_state_dict_strict(
&mut self,
state_dict: &HashMap<String, Tensor>,
) -> Result<()>
fn load_state_dict_strict( &mut self, state_dict: &HashMap<String, Tensor>, ) -> Result<()>
Source§fn state_dict(&self) -> HashMap<String, Tensor>
fn state_dict(&self) -> HashMap<String, Tensor>
Source§fn name(&self) -> Option<&str>
fn name(&self) -> Option<&str>
Source§fn named_children(&self) -> Vec<(String, &dyn Module)>
fn named_children(&self) -> Vec<(String, &dyn Module)>
Source§fn modules(&self) -> Vec<&dyn Module>where
Self: Sized,
fn modules(&self) -> Vec<&dyn Module>where
Self: Sized,
Source§fn named_modules(&self) -> Vec<(String, &dyn Module)>where
Self: Sized,
fn named_modules(&self) -> Vec<(String, &dyn Module)>where
Self: Sized,
Source§fn num_parameters(&self) -> usize
fn num_parameters(&self) -> usize
Source§fn num_trainable_parameters(&self) -> usize
fn num_trainable_parameters(&self) -> usize
Source§fn memory_usage(&self) -> usize
fn memory_usage(&self) -> usize
Source§fn extra_repr(&self) -> String
fn extra_repr(&self) -> String
Source§fn register_hook(
&mut self,
_hook_type: HookType,
_callback: HookCallback,
) -> Option<HookHandle>
fn register_hook( &mut self, _hook_type: HookType, _callback: HookCallback, ) -> Option<HookHandle>
Source§fn remove_hook(&mut self, _hook_type: HookType, _handle: HookHandle) -> bool
fn remove_hook(&mut self, _hook_type: HookType, _handle: HookHandle) -> bool
Source§fn execute_hooks(
&self,
_hook_type: HookType,
_input: &Tensor,
_output: Option<&Tensor>,
) -> Result<()>
fn execute_hooks( &self, _hook_type: HookType, _input: &Tensor, _output: Option<&Tensor>, ) -> Result<()>
Source§fn forward_with_hooks(&self, input: &Tensor) -> Result<Tensor>
fn forward_with_hooks(&self, input: &Tensor) -> Result<Tensor>
Source§fn call(&self, input: &Tensor) -> Result<Tensor>
fn call(&self, input: &Tensor) -> Result<Tensor>
Source§fn apply(&self, input: &Tensor) -> Result<Tensor>
fn apply(&self, input: &Tensor) -> Result<Tensor>
Source§fn has_parameters(&self) -> bool
fn has_parameters(&self) -> bool
Source§fn has_children(&self) -> bool
fn has_children(&self) -> bool
Source§fn parameter_count(&self) -> usize
fn parameter_count(&self) -> usize
Source§fn trainable_parameter_count(&self) -> usize
fn trainable_parameter_count(&self) -> usize
Source§fn memory_usage_mb(&self) -> f64
fn memory_usage_mb(&self) -> f64
Source§fn toggle_training(&mut self)
fn toggle_training(&mut self)
Source§fn sequential_forward(modules: &[&dyn Module], input: Tensor) -> Result<Tensor>where
Self: Sized,
fn sequential_forward(modules: &[&dyn Module], input: Tensor) -> Result<Tensor>where
Self: Sized,
Source§fn batch_forward(&self, inputs: &[Tensor]) -> Result<Vec<Tensor>>
fn batch_forward(&self, inputs: &[Tensor]) -> Result<Vec<Tensor>>
Source§fn conditional_forward(&self, input: &Tensor, condition: bool) -> Result<Tensor>
fn conditional_forward(&self, input: &Tensor, condition: bool) -> Result<Tensor>
Source§fn residual_forward(&self, input: &Tensor) -> Result<Tensor>
fn residual_forward(&self, input: &Tensor) -> Result<Tensor>
Source§fn module_info(&self) -> ModuleInfo
fn module_info(&self) -> ModuleInfo
Source§fn check_training_readiness(&self) -> Result<()>
fn check_training_readiness(&self) -> Result<()>
Source§fn parameter_names_matching(&self, pattern: &str) -> Vec<String>
fn parameter_names_matching(&self, pattern: &str) -> Vec<String>
Source§fn parameters_by_type(&self, param_type: &str) -> HashMap<String, Parameter>
fn parameters_by_type(&self, param_type: &str) -> HashMap<String, Parameter>
Source§fn clone_parameters(&self) -> HashMap<String, Tensor>
fn clone_parameters(&self) -> HashMap<String, Tensor>
Source§fn diagnose(&self) -> ModuleDiagnostics
fn diagnose(&self) -> ModuleDiagnostics
Auto Trait Implementations§
impl !Freeze for DynamicGraph
impl !RefUnwindSafe for DynamicGraph
impl !UnwindSafe for DynamicGraph
impl Send for DynamicGraph
impl Sync for DynamicGraph
impl Unpin for DynamicGraph
impl UnsafeUnpin for DynamicGraph
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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