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

Function ggml_compute_forward_soft_max_f32

ggml.c:10708–10777  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10706// ggml_compute_forward_soft_max
10707
10708static void ggml_compute_forward_soft_max_f32(
10709 const struct ggml_compute_params * params,
10710 const struct ggml_tensor * src0,
10711 struct ggml_tensor * dst) {
10712 GGML_ASSERT(ggml_is_contiguous(src0));
10713 GGML_ASSERT(ggml_is_contiguous(dst));
10714 GGML_ASSERT(ggml_are_same_shape(src0, dst));
10715
10716 if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
10717 return;
10718 }
10719
10720 // TODO: handle transposed/permuted matrices
10721
10722 const int ith = params->ith;
10723 const int nth = params->nth;
10724
10725 const int nc = src0->ne[0];
10726 const int nr = ggml_nrows(src0);
10727
10728 // rows per thread
10729 const int dr = (nr + nth - 1)/nth;
10730
10731 // row range for this thread
10732 const int ir0 = dr*ith;
10733 const int ir1 = MIN(ir0 + dr, nr);
10734
10735 for (int i1 = ir0; i1 < ir1; i1++) {
10736 float *sp = (float *)((char *) src0->data + i1*src0->nb[1]);
10737 float *dp = (float *)((char *) dst->data + i1*dst->nb[1]);
10738
10739#ifndef NDEBUG
10740 for (int i = 0; i < nc; ++i) {
10741 //printf("p[%d] = %f\n", i, p[i]);
10742 assert(!isnan(sp[i]));
10743 }
10744#endif
10745
10746 float max = -INFINITY;
10747 ggml_vec_max_f32(nc, &max, sp);
10748
10749 ggml_float sum = 0.0;
10750
10751 uint16_t scvt;
10752 for (int i = 0; i < nc; i++) {
10753 if (sp[i] == -INFINITY) {
10754 dp[i] = 0.0f;
10755 } else {
10756 // const float val = (sp[i] == -INFINITY) ? 0.0 : exp(sp[i] - max);
10757 ggml_fp16_t s = GGML_FP32_TO_FP16(sp[i] - max);
10758 memcpy(&scvt, &s, sizeof(scvt));
10759 const float val = GGML_FP16_TO_FP32(ggml_table_exp_f16[scvt]);
10760 sum += (ggml_float)val;
10761 dp[i] = val;
10762 }
10763 }
10764
10765 assert(sum > 0.0);

Callers 1

Calls 5

ggml_is_contiguousFunction · 0.70
ggml_are_same_shapeFunction · 0.70
ggml_nrowsFunction · 0.70
ggml_vec_max_f32Function · 0.70
ggml_vec_scale_f32Function · 0.70

Tested by

no test coverage detected