![]() |
NeuZephyr
Simple DL Framework
|
Represents a multi - dimensional shape, typically used in deep learning for tensor dimensions. More...
Public Member Functions | |
Dimension (size_t n, size_t c, size_t h, size_t w) | |
Constructs a Dimension object with specified dimensions and calculates the corresponding strides. | |
Dimension () | |
Constructs a Dimension object with default dimensions. | |
Dimension (const std::vector< size_t > &dims) | |
Constructs a Dimension object using a vector of dimensions. | |
Dimension (const Dimension &other) | |
Copy constructor for the Dimension class. | |
Dimension & | operator= (const Dimension &other) |
Overloads the assignment operator for the Dimension class. | |
size_t | size () const |
Calculates the total number of elements in the Dimension object. | |
size_t | getStride (size_t i) const |
Retrieves the stride value at a specified index within the Dimension object. | |
std::vector< size_t > | getDims () const |
Retrieves the dimensions of the Dimension object as a std::vector. | |
size_t | N () const |
Retrieves the value of the 'n' dimension. | |
size_t | C () const |
Retrieves the value of the 'c' dimension. | |
size_t | H () const |
Retrieves the value of the 'h' dimension. | |
size_t | W () const |
Retrieves the value of the 'w' dimension. | |
size_t & | operator[] (size_t i) |
Overloads the subscript operator to access the dimensions of the Dimension object. | |
const size_t & | operator[] (size_t i) const |
Overloads the subscript operator to access the dimensions of the const Dimension object. | |
bool | operator== (const Dimension &other) const |
Compares two Dimension objects for equality. | |
bool | isBroadcastCompatible (const Dimension &other) const |
Checks if the current Dimension object is broadcast compatible with another Dimension object. | |
bool | reshape (const Dimension &newShape) |
Attempts to reshape the current Dimension object to a new shape. | |
bool | operator!= (const Dimension &other) const |
Overloads the '!=' operator to compare two Dimension objects for inequality. | |
Dimension | Broadcast (const Dimension &other) const |
Performs broadcasting between two Dimension objects and returns the resulting Dimension . | |
void | updateStride () |
Updates the stride values of the Dimension object. | |
Represents a multi - dimensional shape, typically used in deep learning for tensor dimensions.
This class is designed to handle and manipulate multi - dimensional shapes commonly encountered in deep learning applications. It provides various methods for creating, comparing, reshaping, and broadcasting dimensions. The class uses four size_t variables (n
, c
, h
, w
) to store the dimensions and an array stride
to store the corresponding strides.
Dimension
object, including direct specification of n
, c
, h
, w
, using a std::vector<size_t>
, and copy construction.<<
and >>
operators for easy input and output of Dimension
objects.N()
, C()
, H()
, W()
), the number of elements (size()
), strides (getStride()
), and all dimensions as a std::vector<size_t>
(getDims()
).==
and !=
operators to compare two Dimension
objects for equality.isBroadcastCompatible()
to check if two Dimension
objects can be broadcasted to each other, and a Broadcast()
method to perform the actual broadcasting.reshape()
method to change the shape of the Dimension
object.operator[]
are within the valid range (0 - 3). The private checkIndex
method is used internally to enforce this.reshape
method, ensure that the new shape is valid and appropriate for the context.Definition at line 57 of file Dimension.cuh.
nz::data::Dimension::Dimension | ( | size_t | n, |
size_t | c, | ||
size_t | h, | ||
size_t | w ) |
Constructs a Dimension object with specified dimensions and calculates the corresponding strides.
n | The batch size dimension. Memory flow: host-to-object, as the value is passed from the calling code to the object's member variable. |
c | The channel dimension. Memory flow: host-to-object, as the value is passed from the calling code to the object's member variable. |
h | The height dimension. Memory flow: host-to-object, as the value is passed from the calling code to the object's member variable. |
w | The width dimension. Memory flow: host-to-object, as the value is passed from the calling code to the object's member variable. |
This constructor initializes a Dimension
object with the provided batch size (n
), channel count (c
), height (h
), and width (w
). It then calculates the strides for each dimension based on these values. The stride for a dimension represents the number of elements to skip in the underlying data array to move to the next element along that dimension.
Memory Management Strategy:
Dimension
object.Exception Handling Mechanism:
n
, c
, h
, w
) are valid non - negative size_t
values.Relationship with Other Components:
Dimension
object can be used by other parts of the system that rely on the concept of multi - dimensional data layout, such as tensor manipulation functions.n
, c
, h
, w
) are non - negative size_t
values, as negative values may lead to undefined behavior.Definition at line 5 of file Dimension.cu.
nz::data::Dimension::Dimension | ( | ) |
Constructs a Dimension object with default dimensions.
This constructor initializes a Dimension object with default values for batch size (n = 1), channel count (c = 1), height (h = 1), and width (w = 1). It achieves this by delegating the initialization to the four - parameter constructor of the Dimension class.
None. | This is a default constructor, so it does not take any parameters. |
Memory Management Strategy:
Exception Handling Mechanism:
Relationship with Other Components:
Definition at line 12 of file Dimension.cu.
|
explicit |
Constructs a Dimension object using a vector of dimensions.
This constructor initializes a Dimension object by extracting the first four elements from the provided vector of size_t
values. It then delegates the actual initialization to the four - parameter constructor of the Dimension class.
dims | A reference to a std::vector<size_t> containing the dimensions. Memory flow: host - to - object, as the values from the vector are used to initialize the object's member variables. |
Memory Management Strategy:
Exception Handling Mechanism:
dims
has less than four elements, accessing dims[0]
, dims[1]
, dims[2]
, or dims[3]
will result in undefined behavior. The four - parameter constructor may also throw exceptions if the input values are invalid. This constructor does not have its own exception - handling logic.Relationship with Other Components:
dims
contains at least four elements to avoid undefined behavior.Definition at line 15 of file Dimension.cu.
nz::data::Dimension::Dimension | ( | const Dimension & | other | ) |
Copy constructor for the Dimension class.
This constructor creates a new Dimension object by copying the dimensions from an existing Dimension object. It delegates the actual initialization to the four - parameter constructor of the Dimension class.
other | A reference to an existing Dimension object from which the dimensions will be copied. Memory flow: object - to - object, as the values from the existing object are used to initialize the new object. |
Memory Management Strategy:
Exception Handling Mechanism:
Relationship with Other Components:
Definition at line 18 of file Dimension.cu.
Performs broadcasting between two Dimension
objects and returns the resulting Dimension
.
This function checks if the current Dimension
object and the provided other
Dimension
object are broadcast compatible. If they are, it creates a new Dimension
object where each dimension is the maximum of the corresponding dimensions of the two input Dimension
objects. Otherwise, it throws an std::invalid_argument
exception.
other | A constant reference to another Dimension object to perform broadcasting with. Memory flow: host-to-function, as the object is passed from the calling code to the function. |
Dimension
object representing the result of the broadcasting operation. Memory flow: function-to-host, as the result is returned from the function to the calling code.Memory Management Strategy:
Dimension
object (result
) on the stack. The object is automatically destroyed when it goes out of scope.Exception Handling Mechanism:
std::invalid_argument
exception.Relationship with Other Components:
isBroadcastCompatible
method to check the compatibility of the dimensions. It is useful in scenarios where element-wise operations between tensors of different shapes are required, such as in deep learning frameworks.std::invalid_argument | If the dimensions are not broadcast compatible. |
isBroadcastCompatible
method is correctly implemented and that the getDims
method returns the appropriate dimension values.Definition at line 125 of file Dimension.cu.
|
nodiscard |
Retrieves the value of the 'c' dimension.
This function is used to obtain the value of the 'c' dimension from the current object.
None. | This is a member function, so it operates on the current object. |
size_t
value representing the 'c' dimension. Memory flow: function-to-host, as the value is returned from the function to the calling code.Memory Management Strategy:
Exception Handling Mechanism:
Relationship with Other Components:
Definition at line 55 of file Dimension.cu.
|
nodiscard |
Retrieves the dimensions of the Dimension object as a std::vector.
This function creates and returns a std::vector containing the four dimensions (n, c, h, w) of the Dimension object.
None. | This is a member function, so it operates on the current object. |
Memory Management Strategy:
Exception Handling Mechanism:
Relationship with Other Components:
std::bad_alloc | If there is not enough memory to allocate the std::vector. |
Definition at line 47 of file Dimension.cu.
|
nodiscard |
Retrieves the stride value at a specified index within the Dimension object.
This function checks if the given index is within the valid range using the checkIndex
function. If the index is valid, it returns the corresponding stride value; otherwise, it throws an std::out_of_range
exception.
i | The index of the stride value to retrieve. Memory flow: host-to-function, as the index value is passed from the calling code to the function. |
size_t
value representing the stride at the specified index.Memory Management Strategy:
stride
array within the object.Exception Handling Mechanism:
i
is out of range (i.e., checkIndex(i)
returns false
), the function throws an std::out_of_range
exception with the message "Index out of range".Relationship with Other Components:
checkIndex
function to validate the index. The retrieved stride value can be used in other parts of the program for memory access calculations or other operations related to the data layout.std::out_of_range | If the provided index i is out of the valid range. |
checkIndex
function is correctly implemented to accurately validate the index.Definition at line 40 of file Dimension.cu.
|
nodiscard |
Retrieves the value of the 'h' dimension.
This function is used to obtain the value of the 'h' dimension from the current object.
None. | This is a member function, so it operates on the current object. |
size_t
value representing the 'h' dimension. Memory flow: function-to-host, as the value is returned from the function to the calling code.Memory Management Strategy:
Exception Handling Mechanism:
Relationship with Other Components:
Definition at line 59 of file Dimension.cu.
|
nodiscard |
Checks if the current Dimension object is broadcast compatible with another Dimension object.
This function determines whether the dimensions of two objects can be broadcast together according to the broadcasting rules. For each of the first two dimensions, it checks if the dimensions are equal or if one of them is 1. If all checks pass for the first two dimensions, the objects are considered broadcast compatible.
other | A constant reference to another Dimension object to compare with. Memory flow: host-to-function, as the object is passed from the calling code to the function. |
Memory Management Strategy:
Exception Handling Mechanism:
getDims()
method of both objects returns valid dimension arrays.Relationship with Other Components:
Definition at line 101 of file Dimension.cu.
|
nodiscard |
Retrieves the value of the 'n' dimension.
This function is used to obtain the value of the 'n' dimension from the current object.
None. | This is a member function, so it operates on the current object. |
size_t
value representing the 'n' dimension. Memory flow: function-to-host, as the value is returned from the function to the calling code.Memory Management Strategy:
Exception Handling Mechanism:
Relationship with Other Components:
Definition at line 51 of file Dimension.cu.
bool nz::data::Dimension::operator!= | ( | const Dimension & | other | ) | const |
Overloads the '!=' operator to compare two Dimension objects for inequality.
This function determines whether the current Dimension object is not equal to another Dimension object. It achieves this by negating the result of the equality comparison ('==') between the two objects.
other | A constant reference to another Dimension object to compare with. Memory flow: host-to-function, as the object is passed from the calling code to the function. |
Memory Management Strategy:
Exception Handling Mechanism:
Relationship with Other Components:
Definition at line 121 of file Dimension.cu.
Overloads the assignment operator for the Dimension class.
This function assigns the values of an existing Dimension object to the current object. It first checks for self - assignment to avoid unnecessary operations. If the objects are different, it copies the dimensions and strides from the source object to the current object.
other | A reference to an existing Dimension object whose values will be assigned to the current object. Memory flow: object - to - object, as the values from the existing object are copied to the current object. |
Memory Management Strategy:
Exception Handling Mechanism:
Relationship with Other Components:
n
, c
, h
, w
, and stride
in the Dimension class are correctly defined and accessible.Definition at line 21 of file Dimension.cu.
bool nz::data::Dimension::operator== | ( | const Dimension & | other | ) | const |
Compares two Dimension
objects for equality.
This function checks if all corresponding dimensions (n, c, h, w) of the current Dimension
object are equal to those of another Dimension
object.
other | A constant reference to another Dimension object to compare with. Memory flow: host-to-function, as the object is passed from the calling code to the function. |
Dimension
objects are equal. Memory flow: function-to-host, as the result is returned from the function to the calling code.Memory Management Strategy:
Exception Handling Mechanism:
Relationship with Other Components:
Dimension
objects is required, such as in data validation or sorting algorithms.Dimension
objects are properly initialized before calling this function.Definition at line 97 of file Dimension.cu.
|
nodiscard |
Overloads the subscript operator to access the dimensions of the Dimension object.
This non - const version of the subscript operator allows for both accessing and modifying the dimensions of the Dimension object.
i | A size_t value representing the index of the dimension to access. Memory flow: host - to - function, as the index is passed from the calling code to the function. |
size_t
representing the requested dimension. Memory flow: function - to - host, as a reference to the internal dimension is returned to the calling code.Memory Management Strategy:
Exception Handling Mechanism:
i
is not in the range [0, 3]
, a std::out_of_range
exception is thrown.Relationship with Other Components:
std::out_of_range | If the index i is not in the range [0, 3] . |
i
is in the valid range [0, 3]
to avoid exceptions.Definition at line 67 of file Dimension.cu.
|
nodiscard |
Overloads the subscript operator to access the dimensions of the const Dimension object.
This const version of the subscript operator allows for read - only access to the dimensions of the Dimension object.
i | A size_t value representing the index of the dimension to access. Memory flow: host - to - function, as the index is passed from the calling code to the function. |
size_t
representing the requested dimension. Memory flow: function - to - host, as a const reference to the internal dimension is returned to the calling code.Memory Management Strategy:
Exception Handling Mechanism:
i
is not in the range [0, 3]
, a std::out_of_range
exception is thrown.Relationship with Other Components:
std::out_of_range | If the index i is not in the range [0, 3] . |
i
is in the valid range [0, 3]
to avoid exceptions.Definition at line 82 of file Dimension.cu.
bool nz::data::Dimension::reshape | ( | const Dimension & | newShape | ) |
Attempts to reshape the current Dimension
object to a new shape.
This function checks if the size of the new shape is equal to the size of the current Dimension
object. If they are equal, it updates the current object's dimensions (n, c, h, w) to match the new shape and returns true
. Otherwise, it does not modify the current object and returns false
.
newShape | A constant reference to a Dimension object representing the new shape. Memory flow: host-to-function, as the object is passed from the calling code to the function. |
Memory Management Strategy:
Dimension
object.Exception Handling Mechanism:
false
if the reshape is not possible.Relationship with Other Components:
Definition at line 110 of file Dimension.cu.
|
nodiscard |
Calculates the total number of elements in the Dimension object.
This function computes the product of the four dimensions (n
, c
, h
, w
) of the Dimension object, which represents the total number of elements.
None. | This is a member function, so it operates on the current object. |
size_t
value representing the total number of elements in the Dimension object.Memory Management Strategy:
Exception Handling Mechanism:
size_t
.Relationship with Other Components:
n
, c
, h
, and w
are non - negative to avoid unexpected results.Definition at line 36 of file Dimension.cu.
void nz::data::Dimension::updateStride | ( | ) |
Updates the stride values of the Dimension object.
This function calculates and assigns new stride values based on the current values of c, h, and w in the Dimension object. The stride values are used to determine the memory layout and access pattern for multi - dimensional data.
None |
Memory Management Strategy:
stride
of the Dimension
object. It does not allocate or free any dynamic memory.Exception Handling Mechanism:
c
, h
, and w
are properly initialized.Relationship with Other Components:
c
, h
, and w
are correctly initialized before calling this function.Definition at line 136 of file Dimension.cu.
|
nodiscard |
Retrieves the value of the 'w' dimension.
This function is used to obtain the value of the 'w' dimension from the current object.
None. | This is a member function, so it operates on the current object. |
size_t
value representing the 'w' dimension. Memory flow: function-to-host, as the value is returned from the function to the calling code.Memory Management Strategy:
Exception Handling Mechanism:
Relationship with Other Components:
Definition at line 63 of file Dimension.cu.