NeuZephyr
Simple DL Framework
nz::nodes::calc::ScalarAddNode Class Reference

Represents a scalar addition operation node in a computational graph. More...

Inheritance diagram for nz::nodes::calc::ScalarAddNode:
Collaboration diagram for nz::nodes::calc::ScalarAddNode:

Public Member Functions

 ScalarAddNode (Node *input, Tensor::value_type scalar)
 Constructor to initialize a ScalarAddNode for scalar addition.
 
void forward () override
 Forward pass for the ScalarAddNode to perform scalar addition.
 
void backward () override
 Backward pass for the ScalarAddNode to propagate gradients.
 
- Public Member Functions inherited from nz::nodes::Node
virtual void print (std::ostream &os) const
 Prints the type, data, and gradient of the node.
 
void dataInject (Tensor::value_type *data, bool grad=false) const
 Injects data into a relevant tensor object, optionally setting its gradient requirement.
 
template<typename Iterator >
void dataInject (Iterator begin, Iterator end, const bool grad=false) const
 Injects data from an iterator range into the output tensor of the InputNode, optionally setting its gradient requirement.
 
void dataInject (const std::initializer_list< Tensor::value_type > &data, bool grad=false) const
 Injects data from a std::initializer_list into the output tensor of the Node, optionally setting its gradient requirement.
 

Detailed Description

Represents a scalar addition operation node in a computational graph.

The ScalarAddNode class performs element-wise addition of a tensor and a scalar value. It is commonly used in computational graphs for offsetting tensor values or applying a bias term in various operations.

Key features:

  • Forward Pass: Adds a scalar value to each element of the input tensor and stores the result in the output tensor.
  • Backward Pass: Propagates gradients from the output tensor back to the input tensor without modification, as the derivative of addition with respect to the input is 1.
  • Shape Preservation: Maintains the shape of the input tensor in the output tensor.
  • Gradient Management: Tracks whether gradients are required for the operation based on the properties of the input tensor.

This class is part of the nz::nodes namespace and facilitates scalar-tensor addition operations in computational graphs.

Note
  • The scalar value is applied consistently across all elements of the input tensor.
  • A warning is issued indicating that scalar operations do not support saving to files, and users are encouraged to use matrix operations for model persistence.

Usage Example:

// Example: Using ScalarAddNode for scalar addition
InputNode input({3, 3}, true); // Create an input node with shape {3, 3}
input.output->fill(10.0f); // Fill the input tensor with value 10.0
ScalarAddNode scalar_add_node(&input, 5.0f); // Add 5.0 to the input tensor
scalar_add_node.forward(); // Perform the forward pass
scalar_add_node.backward(); // Propagate gradients in the backward pass
std::cout << "Output: " << *scalar_add_node.output << std::endl; // Print the result
Represents a scalar addition operation node in a computational graph.
Definition Nodes.cuh:1499
See also
forward() for the scalar addition computation in the forward pass.
backward() for gradient propagation in the backward pass.
Warning
  • Scalar operations are not yet supported for saving to files. Use matrix operations as an alternative.
Author
Mgepahmge (https://github.com/Mgepahmge)
Date
2024/12/05

Definition at line 1499 of file Nodes.cuh.

Constructor & Destructor Documentation

◆ ScalarAddNode()

nz::nodes::calc::ScalarAddNode::ScalarAddNode ( Node * input,
Tensor::value_type scalar )

Constructor to initialize a ScalarAddNode for scalar addition.

The constructor initializes a ScalarAddNode, which performs element-wise addition of a tensor and a scalar value. It establishes the connection between the input node and this node, prepares the output tensor with the appropriate shape and properties, and stores the scalar value for use during forward and backward passes.

Parameters
inputA pointer to the input node. Its output tensor will be added to the scalar value.
scalarThe scalar value to add to each element of the input tensor.
  • The input node is added to the inputs vector to establish the connection in the computational graph.
  • The output tensor is initialized with the same shape as the input tensor, and the requires_grad property is determined based on the input tensor's gradient requirements.
  • A warning is issued indicating that scalar operations do not support saving to files, encouraging the use of matrix operations for models requiring persistence.
Note
  • The scalar value is applied consistently across all elements of the input tensor during the forward pass.
  • This node supports automatic gradient tracking if the input tensor requires gradients.
See also
forward() for the forward pass implementation.
backward() for gradient propagation in the backward pass.
Warning
  • Scalar operations are not yet supported for saving to files. Use matrix operations as an alternative.
Author
Mgepahmge (https://github.com/Mgepahmge)
Date
2024/12/05

Definition at line 235 of file Nodes.cu.

Member Function Documentation

◆ backward()

void nz::nodes::calc::ScalarAddNode::backward ( )
overridevirtual

Backward pass for the ScalarAddNode to propagate gradients.

The backward() method propagates the gradient of the loss from the output tensor back to the input tensor. Since the derivative of addition with respect to the input is 1, the gradient from the output tensor is directly copied to the input tensor's gradient.

  • The method first checks if the input tensor requires gradients. If true, the gradient of the output tensor is copied to the gradient of the input tensor using cudaMemcpy.
  • The operation ensures efficient gradient propagation without any additional computation.
Note
  • The backward pass assumes that the gradient of the output tensor is already computed and properly initialized.
  • This method does not modify the gradient values; it performs a direct transfer.
See also
forward() for the scalar addition computation in the forward pass.
Author
Mgepahmge (https://github.com/Mgepahmge)
Date
2024/12/05

Implements nz::nodes::Node.

Definition at line 251 of file Nodes.cu.

Here is the call graph for this function:

◆ forward()

void nz::nodes::calc::ScalarAddNode::forward ( )
overridevirtual

Forward pass for the ScalarAddNode to perform scalar addition.

The forward() method computes the element-wise addition of the input tensor and the scalar value. It uses a CUDA kernel to efficiently execute the operation in parallel on the GPU.

  • A CUDA kernel (ScalarAdd) is launched to add the scalar value to each element of the input tensor.
  • The grid and block dimensions are dynamically calculated based on the size of the output tensor to optimize GPU parallelism.
  • The result of the addition is stored in the output tensor.
Note
  • The addition operation is performed as output[i] = input[i] + scalar for each element of the tensor.
  • The scalar value is applied uniformly to all elements in the input tensor.
See also
backward() for gradient propagation in the backward pass.
Author
Mgepahmge (https://github.com/Mgepahmge)
Date
2024/12/05

Implements nz::nodes::Node.

Definition at line 245 of file Nodes.cu.

Here is the call graph for this function:

The documentation for this class was generated from the following files: