![]() |
NeuZephyr
Simple DL Framework
|
Represents a computational graph, which manages nodes and the computation flow. More...
Public Member Functions | |
Constructors and Destructors | |
ComputeGraph ()=default | |
Default constructor for the ComputeGraph class. | |
~ComputeGraph ()=default | |
Destructor for the ComputeGraph class. | |
Graph Builders | |
InputNode * | addInput (const Tensor::shape_type &shape, bool requires_grad=false, const std::string &name="default") |
Adds an input node to the computational graph. | |
InputNode * | addInput (const Tensor &tensor, const std::string &name="default") |
Adds an input node to the computational graph using a Tensor. | |
InputNode * | addInput (InputNode *input, const std::string &name="default") |
Adds an existing InputNode to the computational graph. | |
InputNode * | addInput (const Tensor::shape_type &shape, Tensor::value_type *data, bool requires_grad, bool host, const std::string &name="default") |
Adds an input node to the computation graph and returns a pointer to the newly created InputNode. | |
InputNode * | addInput (const Tensor::shape_type &shape, const std::initializer_list< Tensor::value_type > &data, bool requires_grad, const std::string &name="default") |
Adds an InputNode to the ComputeGraph using a std::initializer_list for data and returns a pointer to the created node. | |
template<typename NodeType > | |
NodeType * | addNode (NodeType *node, const std::string &name="default") |
Adds a node of any type to the computational graph. | |
template<typename... Args> | |
Node * | addNode (const std::string &type, const std::string &input1, const std::string &input2, const std::string &name="default", Args... args) |
Adds a node to the computational graph based on the provided node type and inputs. | |
OutputNode * | addOutput (OutputNode *node, const std::string &name="default") |
Adds an output node to the computational graph. | |
Modifiers | |
void | topologicalSort () |
Performs topological sorting on the computational graph. | |
void | zeroGrad () const |
Resets the gradients of all nodes in the computational graph. | |
void | randomize (const std::string &name, unsigned long long seed=0) |
Randomizes the output tensor of a specified node in the computational graph. | |
void | randomize (const Node *node, unsigned long long seed=0) |
Randomizes the output tensor of a specified node in the computational graph. | |
void | randomizeAll () const |
Randomizes the output tensors of all input nodes in the computational graph. | |
void | fill (const std::string &name, Tensor::value_type val) |
Fills the output tensor of a specified node with a given value. | |
void | fill (const Node *node, Tensor::value_type val) |
Fills the output tensor of a specified node with a given value. | |
void | fillAll (Tensor::value_type val) const |
Fills the output tensors of all input nodes with a given value. | |
void | setInput (const std::string &name, Tensor::value_type *data) |
Sets the input data for a specified node in the computational graph. | |
void | setInput (const Node *node, Tensor::value_type *data) |
Sets the input data for a specified node in the computational graph using a node pointer. | |
Getters | |
bool | isSorted () const |
Checks whether the computational graph has been topologically sorted. | |
Tensor::value_type * | getOutput () const |
Retrieves the output data of the first output node in the computational graph. | |
Tensor::value_type * | getOutputHost () const |
Retrieves the output data of the first output node in the computational graph and copies it to host memory. | |
OutputNode * | getOutputNode () const |
Retrieves the first output node in the computational graph. | |
Tensor::value_type | getLoss () const |
Retrieves the loss value from the first output node in the computational graph. | |
std::ostream & | print (std::ostream &os) |
Prints the details of the computational graph to the provided output stream. | |
Node * | operator[] (const std::string &name) |
Retrieves the node associated with the given name in the computational graph. | |
std::string | nodesList () |
Generates a formatted string representing the list of nodes in the ComputeGraph. | |
Computation | |
void | forward () |
Performs forward propagation on the computational graph. | |
void | backward () |
Performs backward propagation on the computational graph. | |
void | update (Optimizer *optimizer) const |
Updates the parameters of the nodes that require gradients using the provided optimizer. | |
File Managers | |
void | save (const std::string &path) |
Saves the current computational graph to a JSON file. | |
void | load (const std::string &path) |
Loads a computational graph from a JSON file. | |
Friends | |
DL_API std::ostream & | operator<< (std::ostream &os, ComputeGraph &graph) |
Overloads the stream insertion operator to print the details of the computational graph. | |
DL_API void | CreateNode (ComputeGraph *graph, const std::string &type, const std::string &name, std::vector< int > pre, const Tensor::shape_type &shape, float *data, bool requires_grad, float *grad) |
Creates and adds a node to the computational graph based on the specified type. | |
Represents a computational graph, which manages nodes and the computation flow.
The ComputeGraph
class is responsible for creating, managing, and computing the flow of nodes in a neural network or any other computational graph. It handles the addition of input and output nodes, as well as performing forward and backward passes through the graph. It also supports gradient updates, randomization of node values, and node management such as saving, loading, and setting node values.
Key features:
forward()
and backward()
methods execute the forward and backward computation across all nodes in the graph.topologicalSort()
method sorts the nodes in a way that respects their dependencies, ensuring that computation happens in the correct order.randomize()
and randomizeAll()
methods.zeroGrad()
method zeros the gradients of all nodes in the graph.save()
and load()
methods allow for the persistence of the graph’s state.This class is designed to be used in a computational graph where nodes represent various mathematical operations and tensors represent the data that flows through the graph.
Type | Reference |
---|---|
Input | nz::nodes::io::InputNode |
Output | nz::nodes::io::OutputNode |
Add | nz::nodes::calc::AddNode |
MatMul | nz::nodes::calc::MatMulNode |
ScalarMul | nz::nodes::calc::ScalarMulNode |
ScalarDiv | nz::nodes::calc::ScalarDivNode |
ScalarAdd | nz::nodes::calc::ScalarAddNode |
ScalarSub | nz::nodes::calc::ScalarSubNode |
Sub | nz::nodes::calc::SubNode |
ReLU | nz::nodes::calc::ReLUNode |
Sigmoid | nz::nodes::calc::SigmoidNode |
Tanh | nz::nodes::calc::TanhNode |
LeakyReLU | nz::nodes::calc::LeakyReLUNode |
Swish | nz::nodes::calc::SwishNode |
ELU | nz::nodes::calc::ELUNode |
HardSigmoid | nz::nodes::calc::HardSigmoidNode |
HardSwish | nz::nodes::calc::HardSwishNode |
Softmax | nz::nodes::calc::SoftmaxNode |
MeanSquaredError | nz::nodes::loss::MeanSquaredErrorNode |
BinaryCrossEntropy | nz::nodes::loss::BinaryCrossEntropyNode |
Definition at line 224 of file ComputeGraph.cuh.
|
default |
Default constructor for the ComputeGraph class.
This constructor initializes the ComputeGraph
object. It sets up all internal data structures, such as the lists for nodes, input nodes, output nodes, and the node roster. The graph is initially empty and requires the addition of nodes and connections to form a complete computational graph.
The constructor doesn't require any arguments and doesn't allocate any resources other than those necessary for the internal structure of the graph.
|
default |
Destructor for the ComputeGraph class.
The destructor ensures that any resources allocated by the ComputeGraph
object, such as the nodes and their associated data, are properly cleaned up when the object is destroyed. It performs any necessary memory deallocation or resource release.
InputNode * nz::graph::ComputeGraph::addInput | ( | const Tensor & | tensor, |
const std::string & | name = "default" ) |
Adds an input node to the computational graph using a Tensor.
This method creates a new InputNode
using the provided Tensor
and adds it to the ComputeGraph
object. The newly created node is added to both the inputNodes
and nodes
lists. Its name is stored in the nodeRoster
and nodeRosterReverse
maps, allowing for easy lookup by name. If no name is provided, a unique name is generated.
tensor | The Tensor object that represents the data for the input node. |
name | The name of the input node. If "default", a unique name is generated. |
InputNode
.name
is "default", a unique name will be generated for the input node by concatenating the node's type and a reference counter.inputNodes
and nodes
, ensuring it is part of the computational graph.Definition at line 319 of file ComputeGraph.cu.
InputNode * nz::graph::ComputeGraph::addInput | ( | const Tensor::shape_type & | shape, |
bool | requires_grad = false, | ||
const std::string & | name = "default" ) |
Adds an input node to the computational graph.
This method creates a new InputNode
with the specified shape and gradient requirements and adds it to the ComputeGraph
object. The newly created node is also added to the inputNodes
list, and its name is recorded in the nodeRoster
and nodeRosterReverse
maps for easy access by name.
shape | The shape of the input tensor for the node. |
requires_grad | A boolean indicating whether the input node requires gradients for backpropagation. |
name | The name of the input node. If not provided, a default name is generated. |
InputNode
.inputNodes
and nodes
, ensuring it is part of the overall computational graph.Definition at line 302 of file ComputeGraph.cu.
InputNode * nz::graph::ComputeGraph::addInput | ( | const Tensor::shape_type & | shape, |
const std::initializer_list< Tensor::value_type > & | data, | ||
bool | requires_grad, | ||
const std::string & | name = "default" ) |
Adds an InputNode to the ComputeGraph using a std::initializer_list for data and returns a pointer to the created node.
shape | A reference to the shape of the tensor associated with the InputNode (host-to-device). Defines the dimensions of the input tensor. |
data | A std::initializer_list containing the initial values for the tensor (host-to-device). |
requires_grad | A boolean indicating whether the tensor of the InputNode should require gradient computation. |
name | A string representing the name of the InputNode. If set to "default", a unique name will be generated. |
This function is responsible for creating and adding an InputNode to the ComputeGraph. Memory management: It uses the new
operator to allocate memory for the InputNode, and the ComputeGraph takes ownership of this memory. The memory should be deallocated when the ComputeGraph is destroyed.
Exception handling: This function does not explicitly catch exceptions. If memory allocation for the InputNode fails (new
throws std::bad_alloc
), or if there are issues with the shape
or data
passed to the InputNode constructor, the exceptions will propagate to the caller.
It interacts with the nodes
, inputNodes
, nodeRoster
, and nodeRosterReverse
members of the ComputeGraph to manage the list of nodes and the mapping between node names and pointers.
std::bad_alloc | If memory allocation for the InputNode fails. |
shape
and data
are compatible with the requirements of the InputNode constructor.name
is "default", a unique name will be generated based on the node type and a reference counter.nodeRoster
map, where m is the number of nodes in the graph.Definition at line 370 of file ComputeGraph.cu.
InputNode * nz::graph::ComputeGraph::addInput | ( | const Tensor::shape_type & | shape, |
Tensor::value_type * | data, | ||
bool | requires_grad, | ||
bool | host, | ||
const std::string & | name = "default" ) |
Adds an input node to the computation graph and returns a pointer to the newly created InputNode.
shape | A reference to the shape of the input tensor (host-to-device). This defines the dimensions of the tensor associated with the input node. |
data | A pointer to the initial data for the input tensor (host-to-device). It can be nullptr if no initial data is provided. |
requires_grad | A boolean indicating whether the input tensor should require gradient computation. |
host | A boolean indicating whether the tensor data is stored on the host. |
name | A string representing the name of the input node. If set to "default", a unique name will be generated. |
This function is used to add an input node to the computation graph. Memory management: It dynamically allocates memory for the InputNode using the new
operator. The caller does not need to free this memory directly as the graph takes ownership of the node. When the graph is destroyed, it should be responsible for deallocating the memory of all its nodes.
Exception handling: This function does not explicitly catch exceptions. If memory allocation for the InputNode fails (new
throws std::bad_alloc
), the exception will propagate to the caller. Also, if there are issues with the provided shape
or data
, the constructor of InputNode may throw relevant exceptions.
This function interacts with the nodes
, inputNodes
, nodeRoster
, and nodeRosterReverse
members of the ComputeGraph
class to manage the list of nodes and the mapping between node names and pointers.
std::bad_alloc | If memory allocation for the InputNode fails. |
shape
and data
are valid and compatible with the requirements of the InputNode constructor.name
is set to "default", a unique name will be generated based on the node type and a reference counter.nodeRoster
map, where m is the number of nodes in the graph.Definition at line 352 of file ComputeGraph.cu.
InputNode * nz::graph::ComputeGraph::addInput | ( | InputNode * | input, |
const std::string & | name = "default" ) |
Adds an existing InputNode
to the computational graph.
This method adds an already created InputNode
to the ComputeGraph
. The node is pushed into both the nodes
and inputNodes
lists. If a name is provided, the node is stored in the nodeRoster
and nodeRosterReverse
maps with the given name. If the name is "default"
, a unique name is generated for the node.
input | A pointer to the existing InputNode to be added to the graph. |
name | The name of the input node. If "default", a unique name is generated for the node. |
InputNode
.name
is "default"
, a unique name is generated by concatenating the node's type and a reference counter.inputNodes
and nodes
lists, ensuring it becomes part of the computational graph.Definition at line 336 of file ComputeGraph.cu.
|
inline |
Adds a node to the computational graph based on the provided node type and inputs.
This method adds a node to the computational graph based on the specified node type (e.g., "Input", "Output", "Add", "MatMul", etc.). It also ensures that the required input nodes are present in the graph. Depending on the node type, the method creates and adds the corresponding node to the graph and returns a pointer to the added node. If the node type or input nodes are invalid, an exception will be thrown.
type | The type of the node to be added. It can be one of the following: "Input", "Output", "Add", "MatMul", "Sub", "ReLU", "Sigmoid", "Tanh", "LeakyReLU", "Swish", "ELU", "HardSigmoid", "HardSwish", "Softmax", "MeanSquaredError", "BinaryCrossEntropy", "ScalarAdd", "ScalarSub", "ScalarMul", "ScalarDiv". |
input1 | The name of the first input node. The exact meaning depends on the node type. |
input2 | The name of the second input node (if required by the node type). |
name | The name of the node to be added. If "default" , a unique name is generated. The default value is "default" . |
args | Additional arguments required for some node types (e.g., parameters for LeakyReLU, ELU, etc.). |
std::runtime_error | If any required input node is not found, or if an unsupported node type is provided. |
nullptr
because these nodes cannot be added using this method.Args... args
) for nodes like LeakyReLU, ELU, and others that may require additional parameters.Definition at line 571 of file ComputeGraph.cuh.
|
inline |
Adds a node of any type to the computational graph.
This template method allows adding a node of any type derived from the Node
class to the computational graph. The node is added to the nodes
list and optionally assigned a name. If the name is "default"
, a unique name is generated using the node's type and a reference counter.
NodeType | The type of the node, which must be derived from Node . This allows the method to work with different types of nodes (e.g., InputNode , OutputNode , etc.). |
node | A pointer to the node to be added to the graph. |
name | The name of the node. If "default" , a unique name will be generated. The default value is "default" . |
name
is "default"
, the method generates a unique name for the node by concatenating the node's type and a reference counter (e.g., Input_1
, Add_2
).nodes
list (for graph traversal) and the nodeRoster
/nodeRosterReverse
maps (for name-based access).Definition at line 511 of file ComputeGraph.cuh.
OutputNode * nz::graph::ComputeGraph::addOutput | ( | OutputNode * | node, |
const std::string & | name = "default" ) |
Adds an output node to the computational graph.
This method adds an OutputNode
to the computational graph. It takes an OutputNode
pointer and an optional name. The node is added to both the nodes
list and the outputNodes
list. If the name is "default"
, a unique name is generated using the node's type and a reference counter.
node | A pointer to the OutputNode to be added to the graph. |
name | The name of the node. If "default" , a unique name will be generated. The default value is "default" . |
OutputNode
.name
is "default"
, the method generates a unique name for the node by concatenating the node's type and a reference counter (e.g., Output_1
, Output_2
).nodes
list and the outputNodes
list for graph traversal and output handling.nodeRoster
and nodeRosterReverse
maps.Definition at line 389 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::backward | ( | ) |
Performs backward propagation on the computational graph.
This method performs backward propagation starting from the output node(s) in the computational graph. The method first checks if the graph is sorted. If the graph is not sorted, a runtime error is thrown. If the graph is sorted, the backward propagation is performed by iterating over the nodes in reverse topological order (i.e., from outputs to inputs). Each node’s backward()
method is called to compute gradients with respect to its inputs.
None |
std::runtime_error | If the graph is not sorted or if there are multiple output nodes or no output node. |
The backward()
method does not automatically sort the graph because backward propagation must correspond to a previously completed forward pass. A forward pass determines the order of operations and ensures that the graph is in a valid state for backward propagation. Automatically sorting the graph would interfere with this flow, as backward propagation is dependent on the state of the graph after forward propagation. Hence, the graph is only processed for backward propagation if it has been sorted and the forward pass has already occurred.
Definition at line 414 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::fill | ( | const Node * | node, |
Tensor::value_type | val ) |
Fills the output tensor of a specified node with a given value.
This method fills the output tensor of a node, identified by its pointer, with a specified value. It checks if the node is present in the graph by searching the node pointer in the nodes
list. If the node is found, it calls the fill
method on the node's output tensor to set all its elements to the provided value. If the node is not found in the graph, an exception is thrown.
node | A pointer to the node whose output tensor will be filled. |
val | The value to fill the output tensor with. |
std::runtime_error | if the node is not found in the graph. |
Definition at line 475 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::fill | ( | const std::string & | name, |
Tensor::value_type | val ) |
Fills the output tensor of a specified node with a given value.
This method fills the output tensor of a node, identified by its name, with a specified value. It looks up the node by name in the nodeRoster
. If the node is found, it calls the fill
method on the node's output tensor, setting all its elements to the provided value. If the node is not found, an exception is thrown.
name | The name of the node whose output tensor will be filled. |
val | The value to fill the output tensor with. |
std::runtime_error | if the node with the specified name is not found in the graph. |
Definition at line 466 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::fillAll | ( | Tensor::value_type | val | ) | const |
Fills the output tensors of all input nodes with a given value.
This method iterates over all input nodes in the computational graph and fills their output tensors with the specified value. It calls the fill
method on each input node's output tensor to set all its elements to the provided value. This operation is performed for every input node in the graph.
val | The value to fill the output tensors of all input nodes with. |
Definition at line 484 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::forward | ( | ) |
Performs forward propagation on the computational graph.
This method performs forward propagation on all nodes in the computational graph. It first ensures the graph is sorted in topological order (if not already sorted), and then propagates the data through each node in the sorted order. Each node's forward()
method is called to compute its output based on its inputs.
None |
isSorted()
method. If the graph is not sorted, it calls the topologicalSort()
method to sort the nodes in topological order before performing the forward propagation.topologicalSort()
, the forward()
method calls each node's forward()
method to compute the node’s output and propagate the result through the graph.Definition at line 405 of file ComputeGraph.cu.
|
nodiscard |
Retrieves the loss value from the first output node in the computational graph.
This method retrieves the loss value computed by the first OutputNode
in the computational graph. The method assumes that there is at least one output node in the graph. If no output nodes exist, a std::runtime_error
is thrown.
std::runtime_error | If no output nodes are present in the graph. |
OutputNode
class that computes the loss.Definition at line 534 of file ComputeGraph.cu.
|
nodiscard |
Retrieves the output data of the first output node in the computational graph.
This method retrieves a pointer to the output data of the first OutputNode
in the computational graph. The output data is stored in the output tensor of the node. It is important to note that the returned pointer points to data that resides in GPU memory.
If no output nodes exist in the graph, a std::runtime_error
is thrown. The method assumes that there is at least one output node in the graph, and will not return a nullptr
.
std::runtime_error | If no output nodes are present in the graph. |
Definition at line 508 of file ComputeGraph.cu.
|
nodiscard |
Retrieves the output data of the first output node in the computational graph and copies it to host memory.
This method retrieves a pointer to the output data of the first OutputNode
in the computational graph. It then copies the data from GPU memory to host memory. The returned pointer points to a memory block in host memory that contains the output data.
If the graph contains no output nodes, a runtime error is thrown. The method assumes that there is at least one output node in the graph; otherwise, it will throw an exception.
The returned pointer points to memory allocated in the host's memory space. The caller is responsible for freeing this memory using free()
once it's done using the data.
std::runtime_error | If no output nodes are present in the graph. |
Definition at line 515 of file ComputeGraph.cu.
|
nodiscard |
Retrieves the first output node in the computational graph.
This method retrieves the first OutputNode
in the computational graph. The method assumes that there is at least one output node in the graph. If no output nodes exist, a std::runtime_error
is thrown.
std::runtime_error | If no output nodes are present in the graph. |
Definition at line 527 of file ComputeGraph.cu.
|
nodiscard |
Checks whether the computational graph has been topologically sorted.
This function checks if the sortedNodes
vector contains the nodes in a valid topologically sorted order. It returns true
if the graph is sorted, meaning that each node appears before any node that depends on it. Otherwise, it returns false
, indicating that the graph is not sorted.
true
if the graph is sorted, false
if not.Definition at line 298 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::load | ( | const std::string & | path | ) |
Loads a computational graph from a JSON file.
This method deserializes the computational graph from the provided JSON file and reconstructs the nodes, their types, names, input-output relationships, shapes, data, gradients, and other relevant information. It validates the file structure and populates the graph accordingly.
std::runtime_error
if the path is empty, the graph is already loaded, or there is an issue opening the file.path | The file path from which the graph should be loaded. |
std::runtime_error | If the path is empty, the graph is already loaded, or file reading fails. |
Definition at line 642 of file ComputeGraph.cu.
std::string nz::graph::ComputeGraph::nodesList | ( | ) |
Generates a formatted string representing the list of nodes in the ComputeGraph.
This function iterates through the nodeRoster
of the ComputeGraph to determine the maximum width required for displaying node names and types. It then constructs a formatted string using std::ostringstream
to present the nodes in a tabular format.
Memory management: The function does not allocate any dynamic memory that needs to be explicitly managed. It uses local variables and the std::ostringstream
which handles its own memory internally.
Exception handling: This function does not explicitly catch exceptions. Exceptions such as std::bad_alloc
may be thrown if there is insufficient memory during the construction of the output string.
This function only depends on the nodeRoster
member of the ComputeGraph
class, which stores the mapping between node names and pointers.
std::bad_alloc | If there is insufficient memory to construct the output string. |
nodeRoster
, as it iterates through the map twice.Definition at line 817 of file ComputeGraph.cu.
Node * nz::graph::ComputeGraph::operator[] | ( | const std::string & | name | ) |
Retrieves the node associated with the given name in the computational graph.
This method overloads the operator[]
to provide access to nodes in the computational graph by their name. It allows for easy retrieval of nodes from the nodeRoster
map. The operator returns a pointer to the node associated with the provided name.
If the node with the specified name is not found, the method will cause undefined behavior as it directly accesses the nodeRoster
map without checking for the node's existence.
name | The name of the node to retrieve. |
nodeRoster
, this method will cause undefined behavior because it directly accesses the map. To safely check for the existence of a node, consider using find()
instead.nodeRoster
map's behavior when accessing an element by key.Definition at line 813 of file ComputeGraph.cu.
std::ostream & nz::graph::ComputeGraph::print | ( | std::ostream & | os | ) |
Prints the details of the computational graph to the provided output stream.
The print
method prints a detailed description of the computational graph, including each node's name, its preceding (input) nodes, following (output) nodes, data, and gradients. If the graph is not sorted, it will automatically perform a topological sort before printing the details. The method assumes that the graph contains at least one output node and prints the loss value of the first output node.
os | The output stream where the graph details will be printed (e.g., std::cout ). |
std::runtime_error | if an error occurs during the process. |
topologicalSort()
to sort the nodes.Definition at line 8 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::randomize | ( | const Node * | node, |
unsigned long long | seed = 0 ) |
Randomizes the output tensor of a specified node in the computational graph.
This method sets the values of the specified node's output tensor to random values using the provided random seed. The method first checks if the given node exists in the graph by searching for it in the list of nodes. If the node exists, it calls the randomize()
method on the node’s output tensor. If the node is not found in the graph, a runtime error is thrown.
node | A pointer to the Node whose output tensor should be randomized. |
seed | The seed value for the random number generator. |
std::runtime_error | If the node is not found in the graph. |
randomize()
method is expected to be defined for the node's output tensor to set its values randomly.seed
value to ensure reproducibility of the randomization process.nodes
list to ensure it is part of the graph.Definition at line 449 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::randomize | ( | const std::string & | name, |
unsigned long long | seed = 0 ) |
Randomizes the output tensor of a specified node in the computational graph.
This method sets the values of the specified node's output tensor to random values using the provided random seed. The method first checks if the node with the given name exists in the graph. If the node exists, it calls the randomize()
method on the node’s output tensor. If the node is not found in the graph, a runtime error is thrown.
name | The name of the node whose output tensor should be randomized. |
seed | The seed value for the random number generator. If not provided, the seed defaults to 0. |
std::runtime_error | If the node with the given name is not found in the graph. |
randomize()
method is expected to be defined for the node's output tensor to set its values randomly.seed
value to ensure reproducibility of the randomization process.Definition at line 440 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::randomizeAll | ( | ) | const |
Randomizes the output tensors of all input nodes in the computational graph.
This method iterates over all input nodes in the graph and randomizes the output tensor for each of them. It uses the current system time (in nanoseconds) as the seed for the random number generator. Each input node is assigned a unique seed by incrementing the base seed for each randomization.
Definition at line 458 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::save | ( | const std::string & | path | ) |
Saves the current computational graph to a JSON file.
This method serializes the entire computational graph into a JSON file at the specified path. It traverses the nodes in the graph and stores their types, names, input-output relationships, shapes, data, gradients (if required), and other relevant information in JSON format. The serialized graph can later be loaded for further processing or visualization.
std::runtime_error
if the path is empty, the graph is not sorted, or if there is any failure during file writing.path | The file path where the graph should be saved. |
std::runtime_error | If the path is empty, the graph is not sorted, or file writing fails. |
Definition at line 553 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::setInput | ( | const Node * | node, |
Tensor::value_type * | data ) |
Sets the input data for a specified node in the computational graph using a node pointer.
This method sets the input data for a node in the computational graph by copying the provided raw data into the node's output tensor. The input data is assumed to be an array of type Tensor::value_type
and will be copied into the output tensor of the specified node. The shape of the output tensor will be used to determine the amount of data to copy.
node | A pointer to the Node whose input data is to be set. |
data | A pointer to the raw input data that will be copied into the node's output tensor. |
std::runtime_error | If the node is not found in the graph. |
Definition at line 499 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::setInput | ( | const std::string & | name, |
Tensor::value_type * | data ) |
Sets the input data for a specified node in the computational graph.
This method sets the input data for a node in the computational graph by copying the provided raw data into the node's output tensor. The input data is assumed to be an array of type Tensor::value_type
and will be copied into the output tensor of the node specified by the name
. The shape of the output tensor will be used to determine the amount of data to copy.
name | The name of the node whose input data is to be set. |
data | A pointer to the raw input data that will be copied into the node's output tensor. |
std::runtime_error | If the node with the specified name is not found in the graph. |
Definition at line 490 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::topologicalSort | ( | ) |
Performs topological sorting on the computational graph.
This function performs topological sorting on the computational graph to order the nodes such that each node appears before any nodes that depend on it. The sorted nodes are stored in the sortedNodes
vector, which allows for a correct computation order during graph traversal. It uses Kahn's algorithm for topological sorting.
std::runtime_error | If the graph contains a cycle, indicating that topological sorting is not possible. |
This method modifies the following member variables:
sortedNodes
: A vector that stores the nodes in topologically sorted order.inDegree
: A map that keeps track of the in-degree (number of incoming edges) for each node.adjList
: A map that stores the adjacency list for each node, representing which nodes depend on it.inDegree
of each node to 0.adjList
for each node and increment the inDegree
of nodes that have incoming edges.sortedNodes
list, and decrement the inDegree
of its adjacent nodes (i.e., nodes that depend on it). If any adjacent node's in-degree becomes 0, it is added to the queue.sortedNodes
does not match the total number of nodes in the graph, a cycle is detected and an exception is thrown.Definition at line 258 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::update | ( | Optimizer * | optimizer | ) | const |
Updates the parameters of the nodes that require gradients using the provided optimizer.
This method iterates through all the nodes in the computational graph and applies the optimizer's update step to the nodes that have their output
tensor marked as requiring gradients. The update is performed by calling the step
method of the provided optimizer for each node.
optimizer | A pointer to the optimizer that will be used to update the parameters. The optimizer's step method is called for each node that requires gradients. |
std::runtime_error | If the optimizer is a null pointer. |
Definition at line 541 of file ComputeGraph.cu.
void nz::graph::ComputeGraph::zeroGrad | ( | ) | const |
Resets the gradients of all nodes in the computational graph.
This method iterates over all nodes in the computational graph and calls the zeroGrad()
method on each node's output tensor to reset its gradient. This is useful to clear the gradients between different backward passes, ensuring that previous gradient values do not accumulate. Typically called at the beginning of each new backward pass to prepare the graph for gradient computation.
None |
zeroGrad()
method to reset gradients.Definition at line 434 of file ComputeGraph.cu.
|
friend |
Creates and adds a node to the computational graph based on the specified type.
This function is used to create various types of nodes in a computational graph based on the provided node type, and then adds the created node to the ComputeGraph
object. The node is initialized with the specified shape, data, and gradient information if needed. It also ensures that the nodes are connected to their previous nodes as specified by the pre
vector.
graph | The ComputeGraph object to which the new node will be added. |
type | A string representing the type of node to be created. Supported types include "Input", "Output", "Add", "MatMul", "Sub", "ReLU", "Sigmoid", "Tanh", "LeakyReLU", "Swish", "ELU", "HardSigmoid", "HardSwish", "Softmax", "MeanSquaredError", "BinaryCrossEntropy". |
name | The name of the node to be added to the graph. |
pre | A vector of integers specifying the indices of the previous nodes (input nodes) that this node depends on. The number of elements in pre and the type of node may vary. |
shape | A vector representing the shape of the node's output tensor. |
data | A pointer to the data to initialize the node's output tensor. |
requires_grad | A boolean flag indicating whether the node requires gradients for backpropagation. |
grad | A pointer to the gradient data for the node's output tensor if requires_grad is true. |
std::runtime_error | If an unsupported node type is provided or if there is a mismatch in node dependencies. |
CreateNode
function automatically handles the creation of nodes, their connection to previous nodes, and the addition of the new node to the graph.pre
vector is used to specify which nodes are required as inputs for the current node, and it may differ in size based on the node type.Definition at line 108 of file ComputeGraph.cu.
|
friend |
Overloads the stream insertion operator to print the details of the computational graph.
This function overloads the <<
operator to provide an easy and intuitive way to print the details of a ComputeGraph
object. It calls the print
method of ComputeGraph
to output the graph's nodes, their connections, data, gradients, and loss to the provided output stream.
os | The output stream to which the graph details will be printed (e.g., std::cout ). |
graph | The ComputeGraph object whose details will be printed. |
Definition at line 56 of file ComputeGraph.cu.