You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(Tensor): add operations for getting min/max values, finding element positions and setting values at specific positions
- Added methods to the Tensor class to retrieve the minimum and maximum values of the tensor.
- Implemented functionality to find the positions of specific elements within the tensor.
- Added the ability to set values at specific positions in the tensor.
- Conducted basic testing to ensure the correctness of these new operations.
Copy file name to clipboardExpand all lines: include/NeuZephyr/Tensor.cuh
+53-20Lines changed: 53 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -823,38 +823,59 @@ namespace nz::data {
823
823
voidtranspose();
824
824
825
825
/**
826
-
* @brief Sets a specific element of the tensor's data to a given value.
826
+
* @brief Sets the value of an element in the tensor or its gradient at a specified position.
827
827
*
828
-
* This function modifies a specific element of the tensor's data stored in GPU memory.
829
-
* The element to be modified is specified by its position in the tensor's shape (given as a 2D index).
830
-
* The function first copies the tensor's data from GPU memory to host memory, modifies the specified element,
831
-
* and then copies the updated data back to the GPU memory.
828
+
* This member function allows you to set the value of a specific element in the tensor or its gradient.
829
+
* It first validates the position and the gradient setting based on the tensor's requirements.
832
830
*
833
-
* @param position A `shape_type` (alias for `std::vector<int>`) representing the 2D index (row, column)
834
-
* of the element to modify.
835
-
* @param value The value to which the specified element will be set.
831
+
* @param position The position in the tensor where the value will be set. Memory location: host - to - device.
832
+
* @param value The value to be set at the specified position. Memory location: host - to - device.
833
+
* @param isGrad A boolean indicating whether to set the value in the gradient or the tensor data. Memory location: host - to - device.
836
834
*
837
-
* This function performs the following steps:
838
-
* 1. It checks if the provided position is valid within the tensor's shape. If not, an exception is thrown.
839
-
* 2. It copies the tensor's data from GPU memory to host memory using `cudaMemcpy`.
840
-
* 3. It modifies the specified element at the given position in the tensor's data.
841
-
* 4. It copies the updated data back to the GPU memory.
835
+
* @return None
836
+
*
837
+
* **Memory Management Strategy**:
838
+
* - A temporary array `data` of size `_size` is allocated on the host using `malloc`.
839
+
* - The data from the device (either tensor data or gradient) is copied to the host using `cuStrm::StreamManager<value_type>::Instance().memcpy`.
840
+
* - After the value is set at the specified position in the host - side data, the updated data is copied back to the device.
841
+
* - The temporary array `data` is freed using `free` to avoid memory leaks.
842
+
*
843
+
* **Exception Handling Mechanism**:
844
+
* - Throws `std::invalid_argument` if the `position` is out of bounds of the tensor's shape.
845
+
* - Throws `std::invalid_argument` if `isGrad` is `true` but the tensor does not require gradients.
846
+
* - If any of the `cuStrm::StreamManager` operations fail, it may lead to undefined behavior as error - checking is not explicitly done in this function.
842
847
*
843
-
* @throws std::invalid_argument If the provided position is out of bounds.
848
+
* **Relationship with Other Components**:
849
+
* - Depends on `cuStrm::StreamManager<value_type>::Instance()` for memory copying and data synchronization operations.
850
+
* - Relies on the `_shape` member variable to validate the position and calculate the index in the data array.
851
+
* - Uses the `_data` and `_grad` member variables to access the tensor data and its gradient.
852
+
*
853
+
* @throws std::invalid_argument When the position is out of bounds or when trying to set the gradient of a tensor that does not require gradients.
844
854
*
845
855
* @note
846
-
* - This function uses memory copying between host and device, which can introduce performance overhead.
847
-
* - The tensor's data is modified on the host first and then copied back to the GPU. This approach may not be
848
-
* the most efficient for large tensors or frequent updates.
856
+
* - The time complexity of this function is O(n) due to the memory copying operations, where n is the number of elements in the tensor (`_size`).
857
+
* - Ensure that the CUDA runtime environment is properly initialized and the device memory is valid before calling this function.
858
+
* - Ensure that the `position` is within the valid range of the tensor's shape to avoid exceptions.
859
+
* - If setting the gradient, ensure that the tensor requires gradients.
860
+
*
861
+
* @warning
862
+
* - If any of the `cuStrm::StreamManager` operations fail, the behavior of this function is undefined.
849
863
*
850
864
* @code
851
865
* ```cpp
852
-
* Tensor tensor({2, 3}); // Create a tensor with shape 2x3
853
-
* tensor.setData(std::vector<int>({1, 2}), 7.5f); // Set the element at position (1, 2) to 7.5f
0 commit comments