Skip to content
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
21 changes: 6 additions & 15 deletions ggml/src/ggml-cpu/ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3534,31 +3534,22 @@ static void ggml_compute_forward_rms_norm_f32(

GGML_ASSERT(eps >= 0.0f);

// TODO: optimize
for (int64_t i03 = 0; i03 < ne03; i03++) {
for (int64_t i02 = 0; i02 < ne02; i02++) {
for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);

ggml_float sum = 0.0;
for (int64_t i00 = 0; i00 < ne00; i00++) {
sum += (ggml_float)(x[i00] * x[i00]);
}

const float mean = sum/ne00;

float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);

memcpy(y, x, ne00 * sizeof(float));
// for (int i00 = 0; i00 < ne00; i00++) {
// y[i00] = x[i00];
// }
float sum = 0.0f;
ggml_vec_dot_f32(ne00, &sum, 0, x, 0, x, 0, 1);
Comment on lines +3543 to +3544
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would change the accumulator from double to float. Are there any possible overflow issues?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was also having similar doubts and not sure what is the answer. I think practically this should be OK, but not sure it is worth breaking the convention to accumulate floats into ggml_float.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this and it turns out ggml_vec_dot_f32 already accumulates as ggml_float and returns the result as a float.

So I didn't see a point using ggml_float again unless I'm missing something here.

Ref:

// scalar
ggml_float sumf = 0.0;
for (int i = 0; i < n; ++i) {
sumf += (ggml_float)(x[i]*y[i]);
}

Do let me know if I should run any tests to determine if this change causes a degradation :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ggml_vec_dot_f32 accumulates in a ggml_float only on the scalar branch. On the vectorized ones it accumulates to a float. Have a look and you will see :)


const float scale = 1.0f/sqrtf(mean + eps);
const float mean = sum / ne00;
const float scale = 1.0f / sqrtf(mean + eps);

// if you hit this, likely you got an inf somewhere earlier
assert(scale > 0.0f);

ggml_vec_cpy_f32(ne00, y, x);
ggml_vec_scale_f32(ne00, y, scale);
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/test-backend-ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7012,6 +7012,10 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
test_cases.emplace_back(new test_sum(GGML_TYPE_F32, it));
}

test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, { 1024, 2, 1, 1 }, false, 1e-6f, false)); // qwen3-0.6b
test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, { 2048, 2, 1, 1 }, false, 1e-5f, false)); // llama-3.2-1b, granite-3.3-2b
test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, { 2880, 2, 1, 1 }, false, 1e-5f, false)); // gpt-oss-20b

return test_cases;
}

Expand Down