mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2025-11-12 10:47:01 +00:00
* oai moe * compat with new checkpoint * add attn sink impl * add rope scaling yarn * logits match with latest transformers code * wip chat template * rm trailing space * use ggml_scale_bias * rm redundant is_swa_all * convert interleaved gate_up * graph : fix activation function to match reference (#7) * vocab : handle o200k_harmony special tokens * ggml : add attention sinks support (#1) * llama : add attn sinks * ggml : add attn sinks * cuda : add attn sinks * vulkan : add support for sinks in softmax remove unnecessary return * ggml : add fused swiglu_oai op (#11) * ggml : add fused swiglu_oai op * Update ggml/src/ggml-cpu/ops.cpp Co-authored-by: Georgi Gerganov <ggerganov@gmail.com> * update CUDA impl * cont : metal impl * add vulkan impl * test-backend-ops : more test cases, clean up * llama : remove unfused impl * remove extra lines --------- Co-authored-by: Georgi Gerganov <ggerganov@gmail.com> --------- Co-authored-by: slaren <slarengh@gmail.com> * repack mxfp4 upon conversion * clean up a bit * enable thinking * add quick hack to render only some special tokens * fix bf16 conversion * remove vocab hack * webui ok * support chat parsing for gpt-oss * fix webui * direct mapping mxfp4, FINALLY * force using mxfp4 * properly use lazy tensor * ggml : add mxfp4 ggml : use e8m0 conversion instead of powf Co-authored-by: Diego Devesa <slarengh@gmail.com> change kvalues_mxfp4 table to match e2m1 (#6) metal : remove quantization for now (not used) cuda : fix disabled CUDA graphs due to ffn moe bias vulkan : add support for mxfp4 cont : add cm2 dequant * ggml : add ggml_add_id (#13) * ggml : add ggml_add_id * add cuda impl * llama : add weight support check for add_id * perf opt * add vulkan impl * rename cuda files * add metal impl * allow in-place ggml_add_id * llama : keep biases on CPU with --cpu-moe * llama : fix compile error ggml-ci * cuda : add fallback for __nv_cvt_e8m0_to_bf16raw ggml-ci * cleanup ggml-ci * sycl : fix supports_op for MXFP4 ggml-ci * fix Unknown reasoning format * ggml-cpu : fix AVX build ggml-ci * fix hip build ggml-ci * cuda : add mxfp4 dequantization support for cuBLAS ggml-ci * ggml-cpu : fix mxfp4 fallback definitions for some architectures ggml-ci * cuda : fix version required for __nv_cvt_e8m0_to_bf16raw --------- Co-authored-by: Xuan Son Nguyen <son@huggingface.co> Co-authored-by: slaren <slarengh@gmail.com>
196 lines
5.2 KiB
Plaintext
196 lines
5.2 KiB
Plaintext
#version 450
|
|
|
|
#extension GL_EXT_control_flow_attributes : enable
|
|
|
|
layout (push_constant) uniform parameter
|
|
{
|
|
uint KX;
|
|
uint KY;
|
|
uint ne00;
|
|
uint ne01;
|
|
uint ne02;
|
|
uint ne12;
|
|
uint ne13;
|
|
uint nb11;
|
|
uint nb12;
|
|
uint nb13;
|
|
float scale;
|
|
float max_bias;
|
|
float m0;
|
|
float m1;
|
|
uint n_head_log2;
|
|
uint nrows_x;
|
|
uint has_sinks;
|
|
} p;
|
|
|
|
#include "types.comp"
|
|
|
|
layout(constant_id = 0) const uint BLOCK_SIZE = 32;
|
|
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
|
|
|
|
layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
|
|
layout (binding = 1) readonly buffer Y {B_TYPE data_b[];};
|
|
layout (binding = 2) readonly buffer Z {float data_c[];};
|
|
layout (binding = 3) buffer D {D_TYPE data_d[];};
|
|
|
|
shared FLOAT_TYPE vals[BLOCK_SIZE];
|
|
|
|
// num_iters is the number of BLOCK_SIZE loop iterations we need to iterate
|
|
// over all the columns. The main function tries to pass a constant here,
|
|
// as if it were a template function, to allow unrolling.
|
|
void soft_max(uint num_iters) {
|
|
const uint tid = gl_LocalInvocationID.x;
|
|
const uint rowx = gl_WorkGroupID.z * 262144 + gl_WorkGroupID.y * 512 + gl_WorkGroupID.x;
|
|
|
|
const uint32_t i03 = rowx / (p.ne01 * p.ne02);
|
|
const uint32_t i02 = (rowx - i03 * p.ne01 * p.ne02) / p.ne01;
|
|
const uint32_t i01 = rowx % p.ne01;
|
|
|
|
uint rowy_start = 0;
|
|
if (p.KY > 0) {
|
|
rowy_start = i01 * p.nb11 + (i02 % p.ne12) * p.nb12 + (i03 % p.ne13) * p.nb13;
|
|
}
|
|
|
|
if (rowx >= p.nrows_x) {
|
|
return;
|
|
}
|
|
|
|
float slope = 1.0f;
|
|
|
|
// ALiBi
|
|
if (p.max_bias > 0.0f) {
|
|
const uint h = (rowx / p.ne01) % p.ne02; // head index
|
|
|
|
const float base = h < p.n_head_log2 ? p.m0 : p.m1;
|
|
const uint exp = h < p.n_head_log2 ? h + 1 : 2*(h - p.n_head_log2) + 1;
|
|
|
|
slope = pow(base, exp);
|
|
}
|
|
|
|
// Find max
|
|
FLOAT_TYPE max_val = p.has_sinks == 0 ? uintBitsToFloat(0xFF800000) : data_c[i02];
|
|
|
|
// Cache values while we compute the max, so we don't need to read them
|
|
// again when we're ready to compute exp(x-max).
|
|
const uint DATA_CACHE_SIZE = 16;
|
|
FLOAT_TYPE data_cache[DATA_CACHE_SIZE];
|
|
|
|
[[unroll]] for (uint col0 = 0, idx = 0; idx < num_iters; col0 += BLOCK_SIZE, ++idx) {
|
|
const uint col = col0 + tid;
|
|
|
|
FLOAT_TYPE a = FLOAT_TYPE(0);
|
|
if (col < p.KX) {
|
|
a = data_a[rowx * p.KX + col];
|
|
}
|
|
|
|
FLOAT_TYPE b = FLOAT_TYPE(0);
|
|
if (p.KY > 0 && col < p.KX) {
|
|
b = data_b[rowy_start + col];
|
|
}
|
|
|
|
FLOAT_TYPE v = a * p.scale + slope * b;
|
|
|
|
if (col < p.KX) {
|
|
max_val = max(max_val, v);
|
|
}
|
|
|
|
if (idx < DATA_CACHE_SIZE) {
|
|
data_cache[idx] = v;
|
|
}
|
|
}
|
|
|
|
// reduce across the workgroup
|
|
vals[tid] = max_val;
|
|
barrier();
|
|
[[unroll]] for (uint s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
|
|
if (tid < s) {
|
|
vals[tid] = max(vals[tid], vals[tid + s]);
|
|
}
|
|
barrier();
|
|
}
|
|
|
|
max_val = vals[0];
|
|
barrier();
|
|
|
|
FLOAT_TYPE sum = FLOAT_TYPE(0.0f);
|
|
|
|
// Compute sum{exp(x - max)}
|
|
[[unroll]] for (uint col0 = 0, idx = 0; idx < num_iters; col0 += BLOCK_SIZE, ++idx) {
|
|
const uint col = col0 + tid;
|
|
|
|
if (col >= p.KX) {
|
|
break;
|
|
}
|
|
|
|
// compute exp(a*scale+b*slope), add it to sum, and cache the new value
|
|
// in data_cache if possible.
|
|
const uint i = rowx * p.KX + col;
|
|
FLOAT_TYPE val;
|
|
if (idx < DATA_CACHE_SIZE) {
|
|
val = exp(data_cache[idx] - max_val);
|
|
} else {
|
|
val = exp(FLOAT_TYPE(data_a[i]) * p.scale + (p.KY > 0 ? slope * FLOAT_TYPE(data_b[rowy_start + col]) : FLOAT_TYPE(0.0f)) - max_val);
|
|
}
|
|
sum += val;
|
|
if (idx < DATA_CACHE_SIZE) {
|
|
data_cache[idx] = val;
|
|
} else {
|
|
data_d[i] = D_TYPE(val);
|
|
}
|
|
}
|
|
|
|
// reduce across the workgroup
|
|
vals[tid] = sum;
|
|
barrier();
|
|
[[unroll]] for (uint s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
|
|
if (tid < s) {
|
|
vals[tid] += vals[tid + s];
|
|
}
|
|
barrier();
|
|
}
|
|
sum = vals[0];
|
|
|
|
if (p.has_sinks != 0) {
|
|
sum += FLOAT_TYPE(exp(FLOAT_TYPE(data_c[i02]) - max_val));
|
|
}
|
|
|
|
FLOAT_TYPE rcpdivisor = 1.0/sum;
|
|
|
|
[[unroll]] for (uint col0 = 0, idx = 0; idx < num_iters; col0 += BLOCK_SIZE, ++idx) {
|
|
const uint col = col0 + tid;
|
|
|
|
if (col >= p.KX) {
|
|
continue;
|
|
}
|
|
|
|
if (idx < DATA_CACHE_SIZE) {
|
|
data_d[rowx*p.KX + col] = D_TYPE(data_cache[idx] * rcpdivisor);
|
|
} else {
|
|
data_d[rowx*p.KX + col] *= D_TYPE(rcpdivisor);
|
|
}
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
// instantiate the soft_max function for several different
|
|
// dimensions, to allow loop unrolling
|
|
uint num_blocks = (p.KX + BLOCK_SIZE - 1) / BLOCK_SIZE;
|
|
if (num_blocks > 32) {
|
|
soft_max(num_blocks);
|
|
} else if (num_blocks > 16) {
|
|
soft_max(32);
|
|
} else if (num_blocks > 8) {
|
|
soft_max(16);
|
|
} else if (num_blocks > 4) {
|
|
soft_max(8);
|
|
} else if (num_blocks == 4) {
|
|
soft_max(4);
|
|
} else if (num_blocks == 3) {
|
|
soft_max(3);
|
|
} else if (num_blocks == 2) {
|
|
soft_max(2);
|
|
} else if (num_blocks == 1) {
|
|
soft_max(1);
|
|
}
|
|
}
|