Fix incorrect use of Vec::set_len in TensorBase::append#1384
Merged
Merged
Fix incorrect use of Vec::set_len in TensorBase::append#1384
Vec::set_len in TensorBase::append#1384Conversation
When `TensorBase::append` was used to extend a tensor along an axis other than zero, it could incorrectly extend the storage length to include uninitialized elements. If a tensor was allocated with capacity for (2, 4) and initial shape (2, 0) and then a new column was appended, the storage length would be expanded to 5, but the initialization status of the elements would be: ``` I U U U I ``` Where I = initialized, U = uninitialized. Fix this by initializing all new elements with a valid value, with a fast path for the case where the tensor is contiguous and we are extending along axis 0. In that case we can avoid initializing the new elements before copying data. This fix does mean that elements may be written multiple times. In order to address that it would be necessary to replace `Vec<T>` with a new storage type which permits initialized and uninitialized elements to be interleaved, or use a tensor with `MaybeUninit<T>` storage and defer marking storage as initialized until the tensor is fully grown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
TensorBase::appendwas used to extend a tensor along an axis other than zero, it could incorrectly extend the storage length to include uninitialized elements. It was hard to trigger a crash due to this because the elements are allCopy, but this is nevertheless UB.If a tensor was allocated with capacity for (2, 4) and initial shape (2, 0) and then a new column was appended, the storage length would be expanded to 5, but the initialization status of the elements would be:
Where I = initialized, U = uninitialized.
Fix this by initializing all new elements with a valid value, with a fast path for the case where the tensor is contiguous and we are extending along axis 0. In that case we can avoid initializing the new elements before copying data.
This fix does mean that elements may be written multiple times. In order to address that it would be necessary to replace
Vec<T>with a new storage type which permits initialized and uninitialized elements to be interleaved, or use a tensor withMaybeUninit<T>storage and defer marking storage as initialized until the tensor is fully grown.