MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / ggml_compute_forward_norm_f32

Function ggml_compute_forward_norm_f32

ggml.c:9169–9218  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9167// ggml_compute_forward_norm
9168
9169static void ggml_compute_forward_norm_f32(
9170 const struct ggml_compute_params * params,
9171 const struct ggml_tensor * src0,
9172 struct ggml_tensor * dst) {
9173 GGML_ASSERT(ggml_are_same_shape(src0, dst));
9174
9175 if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
9176 return;
9177 }
9178
9179 GGML_ASSERT(src0->nb[0] == sizeof(float));
9180
9181 const int ith = params->ith;
9182 const int nth = params->nth;
9183
9184 GGML_TENSOR_UNARY_OP_LOCALS
9185
9186 float eps;
9187 memcpy(&eps, dst->op_params, sizeof(float));
9188
9189 // TODO: optimize
9190 for (int64_t i03 = 0; i03 < ne03; i03++) {
9191 for (int64_t i02 = 0; i02 < ne02; i02++) {
9192 for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
9193 const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
9194
9195 ggml_float sum = 0.0;
9196 for (int64_t i00 = 0; i00 < ne00; i00++) {
9197 sum += (ggml_float)x[i00];
9198 }
9199
9200 float mean = sum/ne00;
9201
9202 float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
9203
9204 ggml_float sum2 = 0.0;
9205 for (int64_t i00 = 0; i00 < ne00; i00++) {
9206 float v = x[i00] - mean;
9207 y[i00] = v;
9208 sum2 += (ggml_float)(v*v);
9209 }
9210
9211 float variance = sum2/ne00;
9212 const float scale = 1.0f/sqrtf(variance + eps);
9213
9214 ggml_vec_scale_f32(ne00, y, scale);
9215 }
9216 }
9217 }
9218}
9219
9220static void ggml_compute_forward_norm(
9221 const struct ggml_compute_params * params,

Callers 1

Calls 2

ggml_are_same_shapeFunction · 0.70
ggml_vec_scale_f32Function · 0.70

Tested by

no test coverage detected