Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: Convert an FDIV to an FMUL in a hot loop #1030

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Convert an FDIV to an FMUL in a hot loop in CPU tensor operation.
- Fixed compilation with clang 16.0.6
- Added Threads::Threads to `EXT_LIBS`
- Updates to pymarian: building for multiple python versions; disabling tcmalloc; hosting gated COMETs on HuggingFace
Expand Down
8 changes: 4 additions & 4 deletions src/tensors/cpu/tensor_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,11 +1110,11 @@ void LayerNormalizationImpl(float* out,
sqSum += ex * ex;
}

float sigma = std::sqrt(sqSum / cols + eps);
float invSigma = 1.0/std::sqrt(sqSum / cols + eps);

#pragma omp simd
for(int i = 0; i < cols; ++i) {
float t = alpha[alphaStride * i] * ((sp[i] - mean) / sigma);
float t = alpha[alphaStride * i] * ((sp[i] - mean) * invSigma);
if(hasBeta)
t += beta[betaStride * i];

Expand Down Expand Up @@ -1295,11 +1295,11 @@ void RMSNormalizationImpl(float* out,
sqSum += sp[i] * sp[i];
}

float rms = std::sqrt(sqSum / cols + eps);
float invRms = 1.0/std::sqrt(sqSum / cols + eps);

#pragma omp simd
for(int i = 0; i < cols; ++i) {
float t = alpha[alphaStride * i] * (sp[i] / rms);
float t = alpha[alphaStride * i] * (sp[i] * invRms);
if(hasBeta)
t += beta[betaStride * i];

Expand Down