Skip to content

Improve element-wise tensor operation performance - #454

Merged
Dobiasd merged 7 commits into
masterfrom
optimize-inference-performance
Mar 13, 2026
Merged

Improve element-wise tensor operation performance#454
Dobiasd merged 7 commits into
masterfrom
optimize-inference-performance

Conversation

@Dobiasd

@Dobiasd Dobiasd commented Mar 13, 2026

Copy link
Copy Markdown
Owner

transform_tensor previously used fplus::transform_convert, which calls reserve() followed by std::transform with a back_insert_iterator. Because push_back updates the vector's internal end pointer on every write, the compiler cannot auto-vectorize the loop.

This PR replaces it with resize() and a plain random-access iterator, allowing GCC to emit SIMD instructions for element-wise operations. A fast path is also added to relu_layer for the common standard ReLU case (max(0, x)), using a simpler single-expression lambda that is easier for the compiler to optimize than the general three-branch version.

Result: ~5% faster forward pass on VGG19 (measured on a single thread, release build, -march=native).

Dobiasd added 7 commits March 13, 2026 10:11
fplus::transform_convert uses reserve() + back_insert_iterator, which
prevents auto-vectorization because push_back updates the vector's
internal end pointer on every write. Using resize() and writing via a
plain random-access iterator allows the compiler to emit SIMD
instructions, giving ~5% faster inference on VGG19.
@Dobiasd
Dobiasd merged commit 5686c8c into master Mar 13, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant