diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-10 15:43:25 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-14 08:27:03 +0200 |
| commit | e8259865f41ceb50dd28b267186d11c62a465135 (patch) | |
| tree | cd5c8a4dff6936c5a9d511f2eea484393b698152 /qwen_kernels.c | |
| parent | 5f73e5dc901f47cda6b2a26eea31cf9fc6178bc7 (diff) | |
| download | ffwd-e8259865f41ceb50dd28b267186d11c62a465135.tar.gz ffwd-e8259865f41ceb50dd28b267186d11c62a465135.zip | |
kernels: delete dead audio/causal-LM leftovers and unused exports
Diffstat (limited to 'qwen_kernels.c')
| -rw-r--r-- | qwen_kernels.c | 499 |
1 files changed, 2 insertions, 497 deletions
diff --git a/qwen_kernels.c b/qwen_kernels.c index bfa0691..85871d4 100644 --- a/qwen_kernels.c +++ b/qwen_kernels.c @@ -186,18 +186,6 @@ void qwen_add_inplace(float *a, const float *b, int n) { for (int i = 0; i < n; i++) a[i] += b[i]; } -void qwen_mul_inplace(float *a, const float *b, int n) { - for (int i = 0; i < n; i++) a[i] *= b[i]; -} - -void qwen_scale(float *x, float s, int n) { - for (int i = 0; i < n; i++) x[i] *= s; -} - -void qwen_copy(float *dst, const float *src, int n) { - memcpy(dst, src, n * sizeof(float)); -} - /* ======================================================================== * Matrix Operations * ======================================================================== */ @@ -585,259 +573,13 @@ void qwen_linear_nobias_bf16(float *y, const float *x, const uint16_t *W_bf16, bf16_linear_rows(y, x, W_bf16, NULL, seq_len, in_dim, out_dim); } -/* Find argmax over a range of output rows [start, end). - * Uses 2-row processing to amortize x vector loads (same as bf16_matvec_fused). */ -static void argmax_bf16_range(const float *x, const uint16_t *W_bf16, - int in_dim, int start, int end, - int *best_out, float *best_val_out) { - qwen_argmax_bf16_range_impl(x, W_bf16, in_dim, start, end, best_out, best_val_out); -} - -typedef struct { - const float *x; - const uint16_t *W_bf16; - int in_dim; - int out_dim; - int best_idx[QWEN_MAX_THREADS]; - float best_val[QWEN_MAX_THREADS]; -} argmax_task_t; - -static void argmax_worker(int tid, int n_threads, void *arg) { - argmax_task_t *t = (argmax_task_t *)arg; - int chunk = (t->out_dim + n_threads - 1) / n_threads; - int start = tid * chunk; - int end = start + chunk; - if (end > t->out_dim) end = t->out_dim; - if (start >= end) { - t->best_val[tid] = -1e30f; - t->best_idx[tid] = 0; - return; - } - argmax_bf16_range(t->x, t->W_bf16, t->in_dim, start, end, - &t->best_idx[tid], &t->best_val[tid]); -} - -int qwen_argmax_matvec_bf16(const float *x, const uint16_t *W_bf16, - int in_dim, int out_dim) { - if (tp.n_threads <= 1) { - int best; - float best_val; - argmax_bf16_range(x, W_bf16, in_dim, 0, out_dim, &best, &best_val); - return best; - } - - argmax_task_t task; - task.x = x; - task.W_bf16 = W_bf16; - task.in_dim = in_dim; - task.out_dim = out_dim; - parallel_for(argmax_worker, &task); - - int best = task.best_idx[0]; - float best_val = task.best_val[0]; - for (int i = 1; i < tp.n_threads; i++) { - if (task.best_val[i] > best_val) { - best_val = task.best_val[i]; - best = task.best_idx[i]; - } - } - return best; -} - - -/* ======================================================================== - * 2D Convolution (im2col + BLAS sgemm) - * ======================================================================== */ - -/* - * im2col: Unroll input patches into a column matrix for GEMM-based conv2d. - * Input: [C_in, H_in, W_in] - * Output columns: [C_in * kH * kW, H_out * W_out] - */ -static void im2col(const float *in, float *cols, - int c_in, int h_in, int w_in, - int kh, int kw, int stride, int padding, - int h_out, int w_out) { - int col_len = h_out * w_out; - for (int ic = 0; ic < c_in; ic++) { - for (int ki = 0; ki < kh; ki++) { - for (int kj = 0; kj < kw; kj++) { - int col_row = (ic * kh + ki) * kw + kj; - float *col_ptr = cols + (size_t)col_row * col_len; - for (int oh = 0; oh < h_out; oh++) { - int ih = oh * stride - padding + ki; - for (int ow = 0; ow < w_out; ow++) { - int iw = ow * stride - padding + kj; - if (ih >= 0 && ih < h_in && iw >= 0 && iw < w_in) { - col_ptr[oh * w_out + ow] = in[ic * h_in * w_in + ih * w_in + iw]; - } else { - col_ptr[oh * w_out + ow] = 0.0f; - } - } - } - } - } - } -} - -void qwen_conv2d(float *out, const float *in, const float *weight, const float *bias, - int c_in, int c_out, int h_in, int w_in, - int kh, int kw, int stride, int padding) { - int h_out = (h_in + 2 * padding - kh) / stride + 1; - int w_out = (w_in + 2 * padding - kw) / stride + 1; - int patch_size = c_in * kh * kw; - int spatial_out = h_out * w_out; - - /* im2col: input -> column matrix [patch_size, spatial_out] */ - float *cols = (float *)malloc((size_t)patch_size * spatial_out * sizeof(float)); - im2col(in, cols, c_in, h_in, w_in, kh, kw, stride, padding, h_out, w_out); - - /* GEMM: weight[c_out, patch_size] @ cols[patch_size, spatial_out] = out[c_out, spatial_out] */ -#ifdef USE_BLAS - cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, - c_out, spatial_out, patch_size, - 1.0f, weight, patch_size, cols, spatial_out, - 0.0f, out, spatial_out); -#else - for (int oc = 0; oc < c_out; oc++) { - for (int s = 0; s < spatial_out; s++) { - float sum = 0.0f; - for (int p = 0; p < patch_size; p++) { - sum += weight[oc * patch_size + p] * cols[p * spatial_out + s]; - } - out[oc * spatial_out + s] = sum; - } - } -#endif - - free(cols); - - /* Add bias */ - if (bias) { - for (int oc = 0; oc < c_out; oc++) { - float b = bias[oc]; - float *row = out + oc * spatial_out; - for (int s = 0; s < spatial_out; s++) { - row[s] += b; - } - } - } -} /* ======================================================================== * Normalization * ======================================================================== */ -void qwen_layer_norm(float *out, const float *x, const float *weight, const float *bias, - int seq_len, int hidden, float eps) { - for (int s = 0; s < seq_len; s++) { - const float *x_row = x + s * hidden; - float *out_row = out + s * hidden; - - /* Compute mean */ -#if defined(__AVX512F__) - __m512 sumv = _mm512_setzero_ps(); - int i = 0; - for (; i + 16 <= hidden; i += 16) { - sumv = _mm512_add_ps(sumv, _mm512_loadu_ps(x_row + i)); - } - float mean = _mm512_reduce_add_ps(sumv); - for (; i < hidden; i++) mean += x_row[i]; -#elif defined(__AVX2__) - __m256 sumv = _mm256_setzero_ps(); - int i = 0; - for (; i + 8 <= hidden; i += 8) { - sumv = _mm256_add_ps(sumv, _mm256_loadu_ps(x_row + i)); - } - __m128 sum128 = _mm_add_ps(_mm256_castps256_ps128(sumv), _mm256_extractf128_ps(sumv, 1)); - sum128 = _mm_hadd_ps(sum128, sum128); - sum128 = _mm_hadd_ps(sum128, sum128); - float mean = _mm_cvtss_f32(sum128); - for (; i < hidden; i++) mean += x_row[i]; -#else - float mean = 0.0f; - for (int i = 0; i < hidden; i++) mean += x_row[i]; -#endif - mean /= hidden; - - /* Compute variance */ -#if defined(__AVX512F__) && defined(__FMA__) - __m512 meanv = _mm512_set1_ps(mean); - __m512 accv = _mm512_setzero_ps(); - int j = 0; - for (; j + 16 <= hidden; j += 16) { - __m512 v = _mm512_sub_ps(_mm512_loadu_ps(x_row + j), meanv); - accv = _mm512_fmadd_ps(v, v, accv); - } - float var = _mm512_reduce_add_ps(accv); - for (; j < hidden; j++) { - float d = x_row[j] - mean; - var += d * d; - } -#elif defined(__AVX2__) && defined(__FMA__) - __m256 meanv = _mm256_set1_ps(mean); - __m256 accv = _mm256_setzero_ps(); - int j = 0; - for (; j + 8 <= hidden; j += 8) { - __m256 v = _mm256_sub_ps(_mm256_loadu_ps(x_row + j), meanv); - accv = _mm256_fmadd_ps(v, v, accv); - } - __m128 acc128 = _mm_add_ps(_mm256_castps256_ps128(accv), _mm256_extractf128_ps(accv, 1)); - acc128 = _mm_hadd_ps(acc128, acc128); - acc128 = _mm_hadd_ps(acc128, acc128); - float var = _mm_cvtss_f32(acc128); - for (; j < hidden; j++) { - float d = x_row[j] - mean; - var += d * d; - } -#else - float var = 0.0f; - for (int i = 0; i < hidden; i++) { - float d = x_row[i] - mean; - var += d * d; - } -#endif - var /= hidden; - - float inv_std = 1.0f / sqrtf(var + eps); -#if defined(__AVX512F__) && defined(__FMA__) - __m512 meanv2 = _mm512_set1_ps(mean); - __m512 invv = _mm512_set1_ps(inv_std); - int k = 0; - for (; k + 16 <= hidden; k += 16) { - __m512 vx = _mm512_sub_ps(_mm512_loadu_ps(x_row + k), meanv2); - __m512 vw = _mm512_loadu_ps(weight + k); - __m512 vb = _mm512_loadu_ps(bias + k); - __m512 v = _mm512_mul_ps(_mm512_mul_ps(vx, invv), vw); - v = _mm512_add_ps(v, vb); - _mm512_storeu_ps(out_row + k, v); - } - for (; k < hidden; k++) { - out_row[k] = (x_row[k] - mean) * inv_std * weight[k] + bias[k]; - } -#elif defined(__AVX2__) && defined(__FMA__) - __m256 meanv2 = _mm256_set1_ps(mean); - __m256 invv = _mm256_set1_ps(inv_std); - int k = 0; - for (; k + 8 <= hidden; k += 8) { - __m256 vx = _mm256_sub_ps(_mm256_loadu_ps(x_row + k), meanv2); - __m256 vw = _mm256_loadu_ps(weight + k); - __m256 vb = _mm256_loadu_ps(bias + k); - __m256 v = _mm256_mul_ps(_mm256_mul_ps(vx, invv), vw); - v = _mm256_add_ps(v, vb); - _mm256_storeu_ps(out_row + k, v); - } - for (; k < hidden; k++) { - out_row[k] = (x_row[k] - mean) * inv_std * weight[k] + bias[k]; - } -#else - for (int i = 0; i < hidden; i++) { - out_row[i] = (x_row[i] - mean) * inv_std * weight[i] + bias[i]; - } -#endif - } -} - +/* Rows split across the pool only when the tensor is big enough to pay + * for the dispatch. */ #define QWEN_RMS_NORM_PARALLEL_ELEMS 262144 static void qwen_rms_norm_range(float *out, const float *x, @@ -1010,22 +752,6 @@ void qwen_rms_norm_per_head(float *x, const float *weight, * Activation Functions * ======================================================================== */ -void qwen_silu(float *x, int n) { - for (int i = 0; i < n; i++) { - float val = x[i]; - x[i] = val / (1.0f + expf(-val)); - } -} - -void qwen_gelu(float *x, int n) { - for (int i = 0; i < n; i++) { - float val = x[i]; - float x3 = val * val * val; - float inner = 0.7978845608028654f * (val + 0.044715f * x3); - x[i] = 0.5f * val * (1.0f + tanhf(inner)); - } -} - void qwen_silu_mul_inplace(float *gate, const float *up, int n) { #if defined(__APPLE__) && defined(USE_BLAS) enum { CHUNK = 4096 }; @@ -1052,72 +778,6 @@ void qwen_silu_mul_inplace(float *gate, const float *up, int n) { #endif } -typedef struct { - float *out; - const float *gate_up; - int seq_len; - int intermediate; -} swiglu_task_t; - -static void swiglu_worker(int tid, int n_threads, void *arg) { - swiglu_task_t *t = (swiglu_task_t *)arg; - int chunk = (t->seq_len + n_threads - 1) / n_threads; - int s0 = tid * chunk; - int s1 = s0 + chunk; - if (s1 > t->seq_len) s1 = t->seq_len; - if (s0 >= s1) return; - - int inter = t->intermediate; - int alias_inplace = (t->out == t->gate_up); - for (int s = s0; s < s1; s++) { - const float *gu = t->gate_up + (size_t)s * 2 * inter; - float *o = t->out + (size_t)s * inter; - if (!alias_inplace) { -#if defined(__APPLE__) && defined(USE_BLAS) - /* Fast path for prefill: vectorized exp(-g) using Accelerate/vForce. */ - for (int j = 0; j < inter; j++) o[j] = -gu[2 * j]; - int n = inter; - vvexpf(o, o, &n); - for (int j = 0; j < inter; j++) { - float g = gu[2 * j]; - float u = gu[2 * j + 1]; - o[j] = (g / (1.0f + o[j])) * u; - } -#else - for (int j = 0; j < inter; j++) { - float g = gu[2 * j]; - float u = gu[2 * j + 1]; - g = g / (1.0f + expf(-g)); /* SiLU */ - o[j] = g * u; - } -#endif - } else { - /* In-place mode (decode seq=1): keep single-pass scalar to avoid alias hazards. */ - for (int j = 0; j < inter; j++) { - float g = gu[2 * j]; - float u = gu[2 * j + 1]; - g = g / (1.0f + expf(-g)); /* SiLU */ - o[j] = g * u; - } - } - } -} - -void qwen_swiglu_multiply(float *out, const float *gate_up, int seq_len, int intermediate) { - swiglu_task_t task = { - .out = out, - .gate_up = gate_up, - .seq_len = seq_len, - .intermediate = intermediate - }; - - if (tp.n_threads > 1 && seq_len >= 2 && intermediate >= 256) { - parallel_for(swiglu_worker, &task); - } else { - swiglu_worker(0, 1, &task); - } -} - void qwen_softmax(float *x, int rows, int cols) { for (int r = 0; r < rows; r++) { float *row = x + r * cols; @@ -1164,146 +824,6 @@ static inline void qwen_vec_scale_add(float *dst, const float *src, float correc qwen_vec_scale_add_impl(dst, src, correction, n); } -void qwen_bidirectional_attention(float *out, const float *Q, const float *K, - const float *V, int seq __attribute__((unused)), - int n_heads, int head_dim, float scale, - const int *window_starts, int n_windows) { - int hidden = n_heads * head_dim; - - for (int h = 0; h < n_heads; h++) { - for (int w = 0; w < n_windows; w++) { - int ws = window_starts[w]; - int we = window_starts[w + 1]; - - for (int i = ws; i < we; i++) { - const float *q_row = Q + i * hidden + h * head_dim; - float *o_row = out + i * hidden + h * head_dim; - - /* Online softmax */ - float max_score = -1e30f; - float sum_exp = 0.0f; - for (int d = 0; d < head_dim; d++) o_row[d] = 0.0f; - - for (int j = ws; j < we; j++) { - const float *k_row = K + j * hidden + h * head_dim; - const float *v_row = V + j * hidden + h * head_dim; - - float score = qwen_dot_f32_fast(q_row, k_row, head_dim) * scale; - - if (score > max_score) { - float correction = expf(max_score - score); - sum_exp = sum_exp * correction + 1.0f; - qwen_vec_scale_add(o_row, v_row, correction, head_dim); - max_score = score; - } else { - float wt = expf(score - max_score); - sum_exp += wt; - qwen_vec_axpy_inplace(o_row, v_row, wt, head_dim); - } - } - - if (sum_exp > 0.0f) { - float inv_sum = 1.0f / sum_exp; - qwen_vec_scale_inplace(o_row, inv_sum, head_dim); - } - } - } - } -} - -static void qwen_causal_attention_heads(float *out, const float *Q, const float *K, - const float *V, int seq_q, int seq_k, - int n_heads, int n_kv_heads, int head_dim, - float scale, int q_offset, - int head_start, int head_end) { - int heads_per_kv = n_heads / n_kv_heads; - int q_hidden = n_heads * head_dim; - int kv_hidden = n_kv_heads * head_dim; - - for (int h = head_start; h < head_end; h++) { - int kv_h = h / heads_per_kv; - - for (int i = 0; i < seq_q; i++) { - const float *q_row = Q + i * q_hidden + h * head_dim; - float *o_row = out + i * q_hidden + h * head_dim; - int global_pos = q_offset + i; - int k_end = global_pos + 1; - if (k_end > seq_k) k_end = seq_k; - - float max_score = -1e30f; - float sum_exp = 0.0f; - for (int d = 0; d < head_dim; d++) o_row[d] = 0.0f; - - for (int j = 0; j < k_end; j++) { - const float *k_row = K + j * kv_hidden + kv_h * head_dim; - const float *v_row = V + j * kv_hidden + kv_h * head_dim; - - float score = qwen_dot_f32_fast(q_row, k_row, head_dim) * scale; - - if (score > max_score) { - float correction = expf(max_score - score); - sum_exp = sum_exp * correction + 1.0f; - qwen_vec_scale_add(o_row, v_row, correction, head_dim); - max_score = score; - } else { - float wt = expf(score - max_score); - sum_exp += wt; - qwen_vec_axpy_inplace(o_row, v_row, wt, head_dim); - } - } - - if (sum_exp > 0.0f) { - float inv_sum = 1.0f / sum_exp; - qwen_vec_scale_inplace(o_row, inv_sum, head_dim); - } - } - } -} - -typedef struct { - float *out; - const float *Q; - const float *K; - const float *V; - int seq_q, seq_k; - int n_heads, n_kv_heads; - int head_dim; - float scale; - int q_offset; -} causal_attn_task_t; - -static void causal_attn_worker(int tid, int n_threads, void *arg) { - causal_attn_task_t *t = (causal_attn_task_t *)arg; - int chunk = (t->n_heads + n_threads - 1) / n_threads; - int h0 = tid * chunk; - int h1 = h0 + chunk; - if (h1 > t->n_heads) h1 = t->n_heads; - if (h0 >= h1) return; - - qwen_causal_attention_heads(t->out, t->Q, t->K, t->V, - t->seq_q, t->seq_k, t->n_heads, t->n_kv_heads, - t->head_dim, t->scale, t->q_offset, h0, h1); -} - -void qwen_causal_attention(float *out, const float *Q, const float *K, const float *V, - int seq_q, int seq_k, int n_heads, int n_kv_heads, - int head_dim, float scale, int q_offset) { - if (tp.n_threads > 1 && n_heads >= 2 && (seq_q >= 2 || seq_k >= 128)) { - causal_attn_task_t task = { - .out = out, .Q = Q, .K = K, .V = V, - .seq_q = seq_q, .seq_k = seq_k, - .n_heads = n_heads, .n_kv_heads = n_kv_heads, - .head_dim = head_dim, .scale = scale, .q_offset = q_offset - }; - parallel_for(causal_attn_worker, &task); - return; - } - - qwen_causal_attention_heads(out, Q, K, V, - seq_q, seq_k, n_heads, n_kv_heads, - head_dim, scale, q_offset, 0, n_heads); -} - #define QWEN_PACKED_ATTN_QUERY_TILE 32 #define QWEN_PACKED_ATTN_BLAS_MIN_SEQ 128 @@ -1499,21 +1019,6 @@ void qwen_bidirectional_gqa_attention_packed(float *out, const float *Q, * Position Embeddings * ======================================================================== */ -void qwen_sinusoidal_pe(float *pe, int n_pos, int d_model) { - int half = d_model / 2; - float log_timescale = logf(10000.0f) / (float)(half - 1); - - for (int p = 0; p < n_pos; p++) { - float *row = pe + p * d_model; - for (int d = 0; d < half; d++) { - float inv_timescale = expf(-(float)d * log_timescale); - float angle = (float)p * inv_timescale; - row[d] = sinf(angle); /* first half: sin */ - row[half + d] = cosf(angle); /* second half: cos */ - } - } -} - void qwen_compute_rope_neox(float *cos_out, float *sin_out, const int *positions, int seq, int head_dim, float theta) { int half = head_dim / 2; |
