![]() |
NeuZephyr
Simple DL Framework
|
Implements tensor shape transformation within a neural network computational graph. More...
Public Member Functions | |
ReshapeNode (Node *input, const Tensor::shape_type &newShape) | |
Constructs a ReshapeNode object to reshape the input tensor. | |
void | forward () override |
Performs the forward pass operation of the ReshapeNode. | |
void | backward () override |
Performs the backward propagation for the ReshapeNode. | |
![]() | |
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. | |
Implements tensor shape transformation within a neural network computational graph.
The ReshapeNode
class modifies the dimensional structure of input tensors while preserving their underlying data. This node enables flexible tensor shape adaptation between different network layers without altering the actual data values.
Core functionality and behavior:
Implementation specifics:
Typical applications:
Critical considerations:
nz::nodes::calc::ReshapeNode::ReshapeNode | ( | Node * | input, |
const Tensor::shape_type & | newShape ) |
Constructs a ReshapeNode object to reshape the input tensor.
This constructor initializes a ReshapeNode with an input node and a new shape. It checks if the number of dimensions of the input tensor's shape matches the number of dimensions of the new shape. If they match, it adds the input node to the list of inputs, creates a new output tensor with the specified new shape and the same requiresGrad property as the input tensor, and sets the node type to "Reshape".
input | A pointer to the input node. Memory location: host - to - device (used to access input tensor information). |
newShape | A reference to the new shape of type Tensor::shape_type . Memory location: host - to - device (used to define the new shape). |
Memory Management Strategy:
inputs
vector stores a pointer to the input node. The memory management of the input node is assumed to be handled by the caller.output
member variable is a std::shared_ptr
to a new Tensor
object. The memory of the Tensor
will be automatically managed by the std::shared_ptr
.Exception Handling Mechanism:
std::invalid_argument
if the number of dimensions of the input tensor's shape does not match the number of dimensions of the new shape.Relationship with Other Components:
input
node to access its output tensor.Tensor
object for the output.std::invalid_argument | When the number of dimensions of the input tensor's shape and the new shape do not match. |
Node
object.Definition at line 550 of file Nodes.cu.
|
overridevirtual |
Performs the backward propagation for the ReshapeNode.
None |
This function is responsible for performing the backward propagation in the ReshapeNode. If the output of the first input tensor requires gradient computation, it copies the gradient of the output tensor to the gradient of the first input tensor's output. The memory copy operation is performed using the CUDA stream manager with a device-to-device memory transfer.
Memory management strategy: The function does not allocate or free any memory. It only copies existing memory using CUDA's memcpy. Exception handling mechanism: There is no explicit exception handling in this function. However, CUDA's memcpy operation may throw errors if there are issues with the memory pointers or the CUDA device.
Implements nz::nodes::Node.
Definition at line 565 of file Nodes.cu.
|
overridevirtual |
Performs the forward pass operation of the ReshapeNode.
This function copies the data from the output tensor of the input node to the output tensor of the ReshapeNode using CUDA memory copy. It uses the singleton instance of cuStrm::StreamManager<float>
to manage the CUDA stream for the memory copy operation.
None |
Memory Management Strategy:
cudaMemcpyDeviceToDevice
). The source and destination memory are managed by the Tensor
objects (inputs[0]->output
and output
).Exception Handling Mechanism:
Relationship with Other Components:
cuStrm::StreamManager<float>
singleton to manage the CUDA stream for the memory copy.inputs[0]->output
tensor for the source data and the output
tensor for the destination data.None | explicitly, but CUDA errors may occur during the memory copy operation. |
Implements nz::nodes::Node.
Definition at line 559 of file Nodes.cu.