remove one Vec from Tensor - #31
Open
Human9000-bit wants to merge 1 commit into
Open
Conversation
compute_strides() returned Option<Vec<usize>>, so every Tensor constructor -- from_vec, from_aligned, from_f16, from_bf16, filled, zeros, reshape, into_reshape -- heap-allocated a three-to-five element Vec, had DimsVec::from memcpy it into the inline buffer, and freed it again. DimsVec exists precisely to keep shape and strides off the heap for rank <= 6, so the stride path was paying one malloc/free per tensor to populate a structure designed to avoid exactly that. compute_strides now fills a [usize; INLINE_CAP] and returns the DimsVec directly, keeping the old vec! path only for rank > 6, where DimsVec spills to Heap anyway. The eight call sites move the value into the struct instead of converting it. DimsVec itself is untouched: rank > 6 still works and size_of::<Tensor>() is still 144 bytes. A first attempt that wrote the strides through a DerefMut on DimsVec was slower than the Vec it replaced (37.8 ns/op on the reshape bench) because every index write re-matched the enum discriminant. The win needs the plain array, so keep it that way. Validation: cargo test -p yscv-tensor, 228 passed; scripts/check-ci-local.sh passed. Microbench on a 12th Gen Core i5-12500H, pinned with taskset -c 2, performance governor, min of five interleaved runs against the unpatched binary: reshape of a 4-D tensor 33.6 -> 30.9 ns/op (-8 %), from_vec of a rank-3 tensor 51.0 -> 48.0 ns/op (-6 %), clone unchanged at 13.2 ns/op.
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.
I really disliked
Vecs out there in tensor, so I inlined strides.Benches: tensor ops -8% avg, onnx bench -3% avg