Compare commits

..
6 Commits
Author SHA1 Message Date
Aleksander Grygier a6ac41c757 ui: match tool call timer font size to its title (#32922)
Assisted-by: Claude (Opus 5.5 Medium)
2026-11-24 09:25:18 +01:00
Aleksander Grygier 3fccd98e9d ui: let the chat form's model selection decide the submit state (#32920)
When the last assistant message came from a model that is no longer
available, the submit button stayed red and disabled even after an
available model was picked in the form, since availability checked the
conversation's model first. The form's selection now decides it; the
conversation's model only counts when nothing is selected.

Assisted-by: Claude (Opus 5.5 Medium)
2026-11-24 09:25:04 +01:00
Aman Gupta 4af4bb6d8a rpc : reuse one context for request tensors on the server (#32896) 2026-11-24 09:43:50 +02:00
Georgi GerganovandIsNot-10 b12f22d2f2 ggml : use a range-reduced log1p for softplus (#32655)
* metal : use a range-reduced log1p for softplus

Use the atanh series for x in [-0.5, 1] and precise::log(1 + x) otherwise.
This avoids the cancellation in log(1 + exp(x)) for negative inputs and does
not rely on fast-math folding of (1 + e) - 1.

Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)

* tests : add a softplus test for negative inputs

Use inputs in [-16.6, -16.0], where log(1 + exp(x)) has up to ~2x
relative error. This catches both the old form and the fast-math-folded
compensated form.

Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)

* ggml : use log1pf in ggml_compute_softplus_f32

Use log1pf(expf(input)) so the host helper keeps the true value for
x < -16.64.

Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
Co-authored-by: IsNot-10 <209144869@qq.com>

* vulkan : use a range-reduced log1p for softplus

Use the atanh series for x in [-0.5, 1] and log(1 + x) otherwise.

Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
Co-authored-by: IsNot-10 <209144869@qq.com>

* opencl : use a range-reduced log1p for softplus

Use the atanh series for x in [-0.5, 1] and log(1 + x) otherwise.

Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)
Co-authored-by: IsNot-10 <209144869@qq.com>

* vulkan : move log1p to utils.glsl

Move the range-reduced log1p out of unary.comp into utils.glsl so that
other ops such as topk_moe and ssm_scan can include it.  The body is
unchanged; only the call site in op_softplus is renamed.

Co-authored-by: IsNot-10 <209144869@qq.com>
Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)

---------

Co-authored-by: IsNot-10 <209144869@qq.com>
2026-11-24 09:30:43 +02:00
losewayy d141011c25 vulkan: support state size 96 in ssm_scan (#32162)
* vulkan: support state size 96 in ssm_scan

Assisted-by: Claude Code

* vulkan: specialize ssm_scan state size

Assisted-by: Codex

* vulkan: clarify ssm_scan constant names

Assisted-by: Codex
2026-11-24 08:25:31 +01:00
lhez 12d47ba2f4 opencl: fix GDN QK offsets (#32747)
Assisted-by: Codex
2026-11-23 21:14:37 -08:00
17 changed files with 389 additions and 142 deletions
+1 -1
View File
@@ -105,7 +105,7 @@ static inline bool ggml_impl_is_view(const struct ggml_tensor * t) {
}
static inline float ggml_compute_softplus_f32(float input) {
return (input > 20.0f) ? input : logf(1 + expf(input));
return (input > 20.0f) ? input : log1pf(expf(input));
}
// compute correction dims for YaRN RoPE scaling
+20 -1
View File
@@ -21,6 +21,25 @@ inline bfloat4 ggml_metal_unary_cast<bfloat4, float>(float x) {
}
#endif
template <typename T>
inline T ggml_metal_log1p(T x) {
const T y = x / (T(2.0f) + x);
const T y2 = y * y;
T p = T(1.0f / 13.0f);
p = p * y2 + T(1.0f / 11.0f);
p = p * y2 + T(1.0f / 9.0f);
p = p * y2 + T(1.0f / 7.0f);
p = p * y2 + T(1.0f / 5.0f);
p = p * y2 + T(1.0f / 3.0f);
p = p * y2 + T(1.0f);
const T s = T(2.0f) * y * p;
const T l = precise::log(T(1.0f) + x);
const auto in_series = (x >= T(-0.5f)) & (x <= T(1.0f));
const auto tiny = fabs(x) < T(2.5e-38f);
const T r = select(l, s, in_series);
return select(r, x, tiny);
}
template <typename T0, typename T, typename TC>
kernel void kernel_unary_impl(
constant ggml_metal_kargs_unary & args,
@@ -166,7 +185,7 @@ kernel void kernel_unary_impl(
}
if (FC_OP == OP_UNARY_NUM_SOFTPLUS) {
dst_ptr[i0] = ggml_metal_unary_cast<T>(select(log(1 + exp(x)), x, x > 20));
dst_ptr[i0] = ggml_metal_unary_cast<T>(select(ggml_metal_log1p(precise::exp(x)), x, x > TC(20.0f)));
}
if (FC_OP == OP_UNARY_NUM_EXPM1) {
+6
View File
@@ -40664,6 +40664,9 @@ static void ggml_cl_gated_delta_net(ggml_backend_t backend, ggml_tensor * dst) {
const cl_uint sq1 = (cl_uint)(src_q->nb[1] / sizeof(float));
const cl_uint sq2 = (cl_uint)(src_q->nb[2] / sizeof(float));
const cl_uint sq3 = (cl_uint)(src_q->nb[3] / sizeof(float));
const cl_uint sk1 = (cl_uint)(src_k->nb[1] / sizeof(float));
const cl_uint sk2 = (cl_uint)(src_k->nb[2] / sizeof(float));
const cl_uint sk3 = (cl_uint)(src_k->nb[3] / sizeof(float));
const cl_uint sv1 = (cl_uint)(src_v->nb[1] / sizeof(float));
const cl_uint sv2 = (cl_uint)(src_v->nb[2] / sizeof(float));
const cl_uint sv3 = (cl_uint)(src_v->nb[3] / sizeof(float));
@@ -40714,6 +40717,9 @@ static void ggml_cl_gated_delta_net(ggml_backend_t backend, ggml_tensor * dst) {
CL_CHECK(clSetKernelArg(kernel, idx++, sizeof(cl_uint), &sq1));
CL_CHECK(clSetKernelArg(kernel, idx++, sizeof(cl_uint), &sq2));
CL_CHECK(clSetKernelArg(kernel, idx++, sizeof(cl_uint), &sq3));
CL_CHECK(clSetKernelArg(kernel, idx++, sizeof(cl_uint), &sk1));
CL_CHECK(clSetKernelArg(kernel, idx++, sizeof(cl_uint), &sk2));
CL_CHECK(clSetKernelArg(kernel, idx++, sizeof(cl_uint), &sk3));
CL_CHECK(clSetKernelArg(kernel, idx++, sizeof(cl_uint), &sv1));
CL_CHECK(clSetKernelArg(kernel, idx++, sizeof(cl_uint), &sv2));
CL_CHECK(clSetKernelArg(kernel, idx++, sizeof(cl_uint), &sv3));
@@ -95,6 +95,7 @@ kernel void kernel_gated_delta_net(
uint n_seqs,
uint s_off,
uint sq1, uint sq2, uint sq3,
uint sk1, uint sk2, uint sk3,
uint sv1, uint sv2, uint sv3,
uint sb1, uint sb2, uint sb3,
uint H_k,
@@ -136,6 +137,7 @@ kernel void kernel_gated_delta_net(
// input state holds s0 only [S_v, S_v, H, n_seqs]: per-seq stride is H*D.
const uint state_base = (seq_id * H_v + head_id) * state_size;
const uint q_off_base = iq3 * sq3 + iq1 * sq1;
const uint k_off_base = iq3 * sk3 + iq1 * sk1;
const uint v_off_base = seq_id * sv3 + head_id * sv1;
const uint gb_off_base = seq_id * sb3 + head_id * sb1;
const uint state_out_base = (seq_id * H_v + head_id) * state_size;
@@ -231,7 +233,7 @@ kernel void kernel_gated_delta_net(
for (uint t = 0; t < n_tokens; t++) {
const uint q_off = q_off_base + t * sq2;
const uint k_off = q_off;
const uint k_off = k_off_base + t * sk2;
const uint v_off = v_off_base + t * sv2;
const uint gb_off = gb_off_base + t * sb2;
const float beta_val = data_beta[gb_off];
+46 -6
View File
@@ -4,6 +4,46 @@
// softplus
//------------------------------------------------------------------------------
inline float op_log1p(float x) {
const float y = x / (2.0f + x);
const float y2 = y * y;
float p = 1.0f / 13.0f;
p = p * y2 + 1.0f / 11.0f;
p = p * y2 + 1.0f / 9.0f;
p = p * y2 + 1.0f / 7.0f;
p = p * y2 + 1.0f / 5.0f;
p = p * y2 + 1.0f / 3.0f;
p = p * y2 + 1.0f;
const float s = 2.0f * y * p;
const float l = log(1.0f + x);
const float r = select(l, s, (x >= -0.5f) & (x <= 1.0f));
return select(r, x, fabs(x) < 2.5e-38f);
}
inline float4 op_log1p4(float4 x) {
const float4 y = x / ((float4)(2.0f) + x);
const float4 y2 = y * y;
float4 p = (float4)(1.0f / 13.0f);
p = p * y2 + (float4)(1.0f / 11.0f);
p = p * y2 + (float4)(1.0f / 9.0f);
p = p * y2 + (float4)(1.0f / 7.0f);
p = p * y2 + (float4)(1.0f / 5.0f);
p = p * y2 + (float4)(1.0f / 3.0f);
p = p * y2 + (float4)(1.0f);
const float4 s = (float4)(2.0f) * y * p;
const float4 l = log((float4)(1.0f) + x);
const float4 r = select(l, s, (x >= (float4)(-0.5f)) & (x <= (float4)(1.0f)));
return select(r, x, fabs(x) < (float4)(2.5e-38f));
}
inline float softplus_f32(float x) {
return select(op_log1p(exp(x)), x, x > 20.0f);
}
inline float4 softplus_f32_4(float4 x) {
return select(op_log1p4(exp(x)), x, x > (float4)(20.0f));
}
kernel void kernel_softplus_f32(
global const float * src0,
ulong offset0,
@@ -13,7 +53,7 @@ kernel void kernel_softplus_f32(
src0 = (global float*)((global char*)src0 + offset0);
dst = (global float*)((global char*)dst + offsetd);
dst[get_global_id(0)] = (src0[get_global_id(0)] > 20.0f) ? src0[get_global_id(0)] : log(1.0f + exp(src0[get_global_id(0)]));
dst[get_global_id(0)] = softplus_f32(src0[get_global_id(0)]);
}
kernel void kernel_softplus_f32_4(
@@ -25,7 +65,7 @@ kernel void kernel_softplus_f32_4(
src0 = (global float4*)((global char*)src0 + offset0);
dst = (global float4*)((global char*)dst + offsetd);
dst[get_global_id(0)] = (src0[get_global_id(0)] > 20.0f) ? src0[get_global_id(0)] : log(1.0f + exp(src0[get_global_id(0)]));
dst[get_global_id(0)] = softplus_f32_4(src0[get_global_id(0)]);
}
kernel void kernel_softplus_f16(
@@ -38,7 +78,7 @@ kernel void kernel_softplus_f16(
dst = (global half*)((global char*)dst + offsetd);
const float x = convert_float(src0[get_global_id(0)]);
dst[get_global_id(0)] = convert_half_rte((x > 20.0f) ? x : log(1.0f + exp(x)));
dst[get_global_id(0)] = convert_half_rte(softplus_f32(x));
}
kernel void kernel_softplus_f16_4(
@@ -51,7 +91,7 @@ kernel void kernel_softplus_f16_4(
dst = (global half4*)((global char*)dst + offsetd);
const float4 x = convert_float4(src0[get_global_id(0)]);
dst[get_global_id(0)] = convert_half4_rte((x > 20.0f) ? x : log(1.0f + exp(x)));
dst[get_global_id(0)] = convert_half4_rte(softplus_f32_4(x));
}
kernel void kernel_softplus_f32_nc(
@@ -80,7 +120,7 @@ kernel void kernel_softplus_f32_nc(
global const float * x = (global const float *)(src0 + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
global float * y = (global float *)(dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
*y = (*x > 20.0f) ? *x : log(1.0f + exp(*x));
*y = softplus_f32(*x);
}
}
@@ -111,6 +151,6 @@ kernel void kernel_softplus_f16_nc(
global half * hy = (global half *)(dst + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
const float x = convert_float(*hx);
*hy = convert_half_rte((x > 20.0f) ? x : log(1.0f + exp(x)));
*hy = convert_half_rte(softplus_f32(x));
}
}
+35 -98
View File
@@ -1561,6 +1561,13 @@ public:
: backends(std::move(all_backends)), cache_dir(cache_dir) {
stored_graphs.resize(backends.size());
comm_states.resize(backends.size());
ggml_init_params params {
/*.mem_size =*/ ggml_tensor_overhead()*(1 + GGML_MAX_SRC),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
req_ctx.reset(ggml_init(params));
GGML_ASSERT(req_ctx != nullptr);
}
~rpc_server();
@@ -1593,6 +1600,16 @@ public:
};
private:
// empty context for the tensors of one request, valid until the next call
ggml_context * request_ctx() {
ggml_reset(req_ctx.get());
return req_ctx.get();
}
// nullptr if the buffer was not allocated by this server
ggml_backend_buffer_t find_buffer(uint64_t remote_ptr) const {
ggml_backend_buffer_t buffer = reinterpret_cast<ggml_backend_buffer_t>(remote_ptr);
return buffers.find(buffer) != buffers.end() ? buffer : nullptr;
}
void sync_all_backends();
void comm_free_device(uint32_t dev_id);
static bool comm_allreduce_pairwise(void * comm_ctx_v, ggml_tensor ** tensors);
@@ -1631,6 +1648,7 @@ private:
std::vector<comm_state> comm_states;
// SET_TENSOR data is received in chunks through this buffer
std::vector<uint8_t> set_tensor_buf;
ggml_context_ptr req_ctx;
};
void rpc_server::hello(rpc_msg_hello_rsp & response) {
@@ -1647,15 +1665,7 @@ bool rpc_server::get_alloc_size(const rpc_msg_get_alloc_size_req & request, rpc_
return false;
}
ggml_backend_buffer_type_t buft;
struct ggml_init_params params {
/*.mem_size =*/ ggml_tensor_overhead()*(1 + GGML_MAX_SRC),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
ggml_context_ptr ctx_ptr { ggml_init(params) };
GGML_ASSERT(ctx_ptr != nullptr);
ggml_context * ctx = ctx_ptr.get();
ggml_context * ctx = request_ctx();
ggml_tensor * tensor = deserialize_tensor(ctx, &request.tensor);
if (tensor == nullptr) {
@@ -1733,8 +1743,8 @@ bool rpc_server::get_max_size(const rpc_msg_get_max_size_req & request, rpc_msg_
bool rpc_server::buffer_get_base(const rpc_msg_buffer_get_base_req & request, rpc_msg_buffer_get_base_rsp & response) {
LOG_DBG2("[%s] remote_ptr: 0x%" PRIx64 "\n", __func__, request.remote_ptr);
ggml_backend_buffer_t buffer = reinterpret_cast<ggml_backend_buffer_t>(request.remote_ptr);
if (buffers.find(buffer) == buffers.end()) {
ggml_backend_buffer_t buffer = find_buffer(request.remote_ptr);
if (buffer == nullptr) {
LOG_ERROR("[%s] buffer not found\n", __func__);
return false;
}
@@ -1746,8 +1756,8 @@ bool rpc_server::buffer_get_base(const rpc_msg_buffer_get_base_req & request, rp
bool rpc_server::free_buffer(const rpc_msg_free_buffer_req & request) {
sync_all_backends();
LOG_DBG2("[%s] remote_ptr: 0x%" PRIx64 "\n", __func__, request.remote_ptr);
ggml_backend_buffer_t buffer = reinterpret_cast<ggml_backend_buffer_t>(request.remote_ptr);
if (buffers.find(buffer) == buffers.end()) {
ggml_backend_buffer_t buffer = find_buffer(request.remote_ptr);
if (buffer == nullptr) {
LOG_ERROR("[%s] buffer not found\n", __func__);
return false;
}
@@ -1767,8 +1777,8 @@ bool rpc_server::free_buffer(const rpc_msg_free_buffer_req & request) {
bool rpc_server::buffer_clear(const rpc_msg_buffer_clear_req & request) {
sync_all_backends();
LOG_DBG2("[%s] remote_ptr: 0x%" PRIx64 ", value: %u\n", __func__, request.remote_ptr, request.value);
ggml_backend_buffer_t buffer = reinterpret_cast<ggml_backend_buffer_t>(request.remote_ptr);
if (buffers.find(buffer) == buffers.end()) {
ggml_backend_buffer_t buffer = find_buffer(request.remote_ptr);
if (buffer == nullptr) {
LOG_ERROR("[%s] buffer not found\n", __func__);
return false;
}
@@ -1778,15 +1788,7 @@ bool rpc_server::buffer_clear(const rpc_msg_buffer_clear_req & request) {
bool rpc_server::memset_tensor(const rpc_msg_memset_tensor_req & request) {
sync_all_backends();
struct ggml_init_params params {
/*.mem_size =*/ ggml_tensor_overhead(),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
ggml_context_ptr ctx_ptr { ggml_init(params) };
GGML_ASSERT(ctx_ptr != nullptr);
ggml_context * ctx = ctx_ptr.get();
ggml_tensor * tensor = deserialize_tensor(ctx, &request.tensor);
ggml_tensor * tensor = deserialize_tensor(request_ctx(), &request.tensor);
if (!rpc_check_region(__func__, tensor, request.offset, request.size)) {
return false;
}
@@ -1830,10 +1832,7 @@ ggml_tensor * rpc_server::deserialize_tensor(struct ggml_context * ctx, const rp
return nullptr;
}
ggml_backend_buffer_t buffer = reinterpret_cast<ggml_backend_buffer_t>(tensor->buffer);
if (buffer && buffers.find(buffer) == buffers.end()) {
buffer = nullptr;
}
ggml_backend_buffer_t buffer = find_buffer(tensor->buffer);
if (buffer && nbytes > 0 && !rpc_in_buffer(buffer, tensor->data, nbytes)) {
LOG_ERROR("[%s] tensor '%.*s' (op %s, type %s) data 0x%" PRIx64 " with size %" PRIu64 " out of buffer bounds "
"(base %p, size %zu)\n", __func__, (int) sizeof(tensor->name), tensor->name, ggml_op_name((ggml_op) tensor->op),
@@ -1868,15 +1867,7 @@ bool rpc_server::set_tensor(socket_ptr sock, uint64_t input_size) {
const uint64_t offset = request.offset;
const size_t size = input_size - sizeof(request);
struct ggml_init_params params {
/*.mem_size =*/ ggml_tensor_overhead(),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
ggml_context_ptr ctx_ptr { ggml_init(params) };
GGML_ASSERT(ctx_ptr != nullptr);
ggml_context * ctx = ctx_ptr.get();
ggml_tensor * tensor = deserialize_tensor(ctx, &request.tensor);
ggml_tensor * tensor = deserialize_tensor(request_ctx(), &request.tensor);
if (!rpc_check_region(__func__, tensor, offset, size)) {
return false;
}
@@ -1927,15 +1918,7 @@ bool rpc_server::set_tensor_2d(const std::vector<uint8_t> & input) {
return false;
}
struct ggml_init_params params {
/*.mem_size =*/ ggml_tensor_overhead(),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
ggml_context_ptr ctx_ptr { ggml_init(params) };
GGML_ASSERT(ctx_ptr != nullptr);
ggml_context * ctx = ctx_ptr.get();
ggml_tensor * tensor = deserialize_tensor(ctx, &request.tensor);
ggml_tensor * tensor = deserialize_tensor(request_ctx(), &request.tensor);
if (!rpc_check_region(__func__, tensor, request.offset, request.size, request.n_copies, request.stride)) {
return false;
}
@@ -1975,15 +1958,7 @@ bool rpc_server::get_cached_file(uint64_t hash, std::vector<uint8_t> & data) {
bool rpc_server::set_tensor_hash(const rpc_msg_set_tensor_hash_req & request, rpc_msg_set_tensor_hash_rsp & response)
{
sync_all_backends();
struct ggml_init_params params {
/*.mem_size =*/ ggml_tensor_overhead(),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
ggml_context_ptr ctx_ptr { ggml_init(params) };
GGML_ASSERT(ctx_ptr != nullptr);
ggml_context * ctx = ctx_ptr.get();
ggml_tensor * tensor = deserialize_tensor(ctx, &request.tensor);
ggml_tensor * tensor = deserialize_tensor(request_ctx(), &request.tensor);
if (!rpc_check_region(__func__, tensor, request.offset, 0)) {
return false;
}
@@ -2010,15 +1985,7 @@ bool rpc_server::set_tensor_hash(const rpc_msg_set_tensor_hash_req & request, rp
bool rpc_server::init_tensor(const rpc_msg_init_tensor_req & request) {
sync_all_backends();
struct ggml_init_params params {
/*.mem_size =*/ ggml_tensor_overhead(),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
ggml_context_ptr ctx_ptr { ggml_init(params) };
GGML_ASSERT(ctx_ptr != nullptr);
ggml_context * ctx = ctx_ptr.get();
ggml_tensor * tensor = deserialize_tensor(ctx, &request.tensor);
ggml_tensor * tensor = deserialize_tensor(request_ctx(), &request.tensor);
if (tensor == nullptr) {
LOG_ERROR("Null tensor pointer passed to server init_tensor function.\n");
return false;
@@ -2047,15 +2014,7 @@ bool rpc_server::init_tensor(const rpc_msg_init_tensor_req & request) {
bool rpc_server::get_tensor(const rpc_msg_get_tensor_req & request, std::vector<uint8_t> & response) {
sync_all_backends();
struct ggml_init_params params {
/*.mem_size =*/ ggml_tensor_overhead(),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
ggml_context_ptr ctx_ptr { ggml_init(params) };
GGML_ASSERT(ctx_ptr != nullptr);
ggml_context * ctx = ctx_ptr.get();
ggml_tensor * tensor = deserialize_tensor(ctx, &request.tensor);
ggml_tensor * tensor = deserialize_tensor(request_ctx(), &request.tensor);
if (!rpc_check_region(__func__, tensor, request.offset, request.size)) {
return false;
}
@@ -2070,15 +2029,7 @@ bool rpc_server::get_tensor(const rpc_msg_get_tensor_req & request, std::vector<
bool rpc_server::get_tensor_2d(const rpc_msg_tensor_2d_req & request, std::vector<uint8_t> & response) {
sync_all_backends();
struct ggml_init_params params {
/*.mem_size =*/ ggml_tensor_overhead(),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
ggml_context_ptr ctx_ptr { ggml_init(params) };
GGML_ASSERT(ctx_ptr != nullptr);
ggml_context * ctx = ctx_ptr.get();
ggml_tensor * tensor = deserialize_tensor(ctx, &request.tensor);
ggml_tensor * tensor = deserialize_tensor(request_ctx(), &request.tensor);
if (!rpc_check_region(__func__, tensor, request.offset, request.size, request.n_copies, request.stride)) {
return false;
}
@@ -2093,14 +2044,7 @@ bool rpc_server::get_tensor_2d(const rpc_msg_tensor_2d_req & request, std::vecto
bool rpc_server::copy_tensor(const rpc_msg_copy_tensor_req & request, rpc_msg_copy_tensor_rsp & response) {
sync_all_backends();
struct ggml_init_params params {
/*.mem_size =*/ 2*ggml_tensor_overhead(),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
ggml_context_ptr ctx_ptr { ggml_init(params) };
GGML_ASSERT(ctx_ptr != nullptr);
ggml_context * ctx = ctx_ptr.get();
ggml_context * ctx = request_ctx();
ggml_tensor * src = deserialize_tensor(ctx, &request.src);
ggml_tensor * dst = deserialize_tensor(ctx, &request.dst);
@@ -2559,14 +2503,7 @@ bool rpc_server::comm_allreduce(const rpc_msg_comm_allreduce_req & request) {
return false;
}
struct ggml_init_params params = {
/*.mem_size =*/ ggml_tensor_overhead(),
/*.mem_buffer =*/ NULL,
/*.no_alloc =*/ true,
};
ggml_context_ptr ctx_ptr { ggml_init(params) };
GGML_ASSERT(ctx_ptr != nullptr);
ggml_tensor * t_dst = deserialize_tensor(ctx_ptr.get(), &request.tensor);
ggml_tensor * t_dst = deserialize_tensor(request_ctx(), &request.tensor);
if (t_dst == nullptr || t_dst->buffer == nullptr) {
LOG_ERROR("[%s] error deserializing tensor\n", __func__);
return false;
+1
View File
@@ -1018,6 +1018,7 @@ struct vk_device_struct {
vk_pipeline pipeline_gated_delta_net_cm;
vk_pipeline pipeline_gated_delta_net_cm_v32;
vk_pipeline pipeline_gated_delta_net_cm_v128;
vk_pipeline pipeline_ssm_scan_f32_d96;
vk_pipeline pipeline_ssm_scan_f32_d128;
vk_pipeline pipeline_ssm_scan_f32_d256;
vk_pipeline pipeline_ssm_conv_f32;
+13 -8
View File
@@ -4273,12 +4273,15 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
GGML_UNUSED(gdn_shmem);
}
// Pad 96 states to a 128-thread workgroup for 32- and 64-lane subgroups.
if (device->subgroup_arithmetic && device->subgroup_require_full_support) {
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d128, "ssm_scan_128_f32", ssm_scan_subgroup_f32_len, ssm_scan_subgroup_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size}, 1, true, true);
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d256, "ssm_scan_256_f32", ssm_scan_subgroup_f32_len, ssm_scan_subgroup_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {256, device->subgroup_size}, 1, true, true);
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d96, "ssm_scan_96_f32", ssm_scan_subgroup_f32_len, ssm_scan_subgroup_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size, 96}, 1, true, true);
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d128, "ssm_scan_128_f32", ssm_scan_subgroup_f32_len, ssm_scan_subgroup_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size, 128}, 1, true, true);
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d256, "ssm_scan_256_f32", ssm_scan_subgroup_f32_len, ssm_scan_subgroup_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {256, device->subgroup_size, 256}, 1, true, true);
} else {
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d128, "ssm_scan_128_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size, 16}, 1, true, true);
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d256, "ssm_scan_256_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {256, device->subgroup_size, 16}, 1, true, true);
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d96, "ssm_scan_96_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size, 96}, 1, true, true);
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d128, "ssm_scan_128_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size, 128}, 1, true, true);
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d256, "ssm_scan_256_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {256, device->subgroup_size, 256}, 1, true, true);
}
ggml_vk_create_pipeline(device, device->pipeline_ssm_conv_f32, "ssm_conv_f32", ssm_conv_f32_len, ssm_conv_f32_data, "main", 4, sizeof(vk_op_ssm_conv_push_constants), {32, 16, 1}, {32, 16, 0, 0}, 1);
@@ -10253,7 +10256,9 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
case GGML_OP_SSM_SCAN:
if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
const uint32_t d_state = src0->ne[0];
if (d_state == 128) {
if (d_state == 96) {
return ctx->device->pipeline_ssm_scan_f32_d96;
} else if (d_state == 128) {
return ctx->device->pipeline_ssm_scan_f32_d128;
} else if (d_state == 256) {
return ctx->device->pipeline_ssm_scan_f32_d256;
@@ -11523,7 +11528,7 @@ void ggml_vk_ssm_scan(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_te
std::array<uint32_t, 3> elements;
const uint32_t d_state = src0->ne[0];
uint32_t num_subgroups = d_state / ctx->device->subgroup_size;
uint32_t num_subgroups = (d_state == 96 ? 128 : d_state) / ctx->device->subgroup_size;
const uint32_t num_workgroups_x = CEIL_DIV(n_head * head_dim, num_subgroups);
const uint32_t num_workgroups_y = n_seq;
elements = { num_workgroups_x, num_workgroups_y, 1 };
@@ -17273,11 +17278,11 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
return false;
}
if ((d_state != 128 && d_state != 256) || head_dim % 16 != 0) {
if ((d_state != 96 && d_state != 128 && d_state != 256) || head_dim % 16 != 0) {
return false;
}
size_t shmem_size = d_state * sizeof(float);
size_t shmem_size = (d_state == 96 ? 128 : d_state) * sizeof(float);
if (shmem_size > device->properties.limits.maxComputeSharedMemorySize) {
return false;
@@ -8,10 +8,11 @@
#include "types.glsl"
layout(constant_id = 0) const uint D_STATE = 128;
layout(constant_id = 0) const uint BLOCK_SIZE = 128;
layout(constant_id = 1) const uint SUBGROUP_SIZE = 32;
layout(constant_id = 2) const uint D_STATE = 128;
const uint32_t c_factor = D_STATE / SUBGROUP_SIZE;
const uint32_t c_factor = (D_STATE + SUBGROUP_SIZE - 1) / SUBGROUP_SIZE;
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
@@ -46,14 +47,14 @@ float softplus(float x) {
}
#if !USE_SUBGROUP_ADD
shared float temp[D_STATE];
shared float temp[BLOCK_SIZE];
#endif
void main() {
const uint subgroup = gl_SubgroupID;
const uint lane = gl_SubgroupInvocationID;
const uint tid = gl_SubgroupID * SUBGROUP_SIZE + lane;
const uint subgroup_idx = gl_WorkGroupID.x * c_factor + subgroup;
const uint subgroup_idx = gl_WorkGroupID.x * (BLOCK_SIZE / SUBGROUP_SIZE) + subgroup;
const uint head_idx = subgroup_idx / d_head;
const uint head_off = (subgroup_idx % d_head) * 4;
@@ -78,7 +79,8 @@ void main() {
float state[c_factor];
[[unroll]] for (uint j = 0; j < c_factor; j++) {
state[j] = s0[s0_base_idx + SUBGROUP_SIZE * j + lane];
const uint state_idx = SUBGROUP_SIZE * j + lane;
state[j] = D_STATE % SUBGROUP_SIZE == 0 || state_idx < D_STATE ? s0[s0_base_idx + state_idx] : 0.0f;
}
float a = A[A_base_idx];
@@ -91,8 +93,9 @@ void main() {
const float dA = exp(dt_soft_plus * a);
const float x_dt = x[x_base_idx + i * stride_x] * dt_soft_plus;
[[unroll]] for (uint j = 0; j < c_factor; j++) {
float B_val = B[B_base_idx + i * stride_B + SUBGROUP_SIZE * j + lane];
float C_val = C[C_base_idx + i * stride_C + SUBGROUP_SIZE * j + lane];
const uint state_idx = SUBGROUP_SIZE * j + lane;
float B_val = D_STATE % SUBGROUP_SIZE == 0 || state_idx < D_STATE ? B[B_base_idx + i * stride_B + state_idx] : 0.0f;
float C_val = D_STATE % SUBGROUP_SIZE == 0 || state_idx < D_STATE ? C[C_base_idx + i * stride_C + state_idx] : 0.0f;
state[j] = (state[j] * dA) + (B_val * x_dt);
state_sum += state[j] * C_val;
}
@@ -121,7 +124,9 @@ void main() {
if (slot > 0u && slot < K) {
const uint snapshot_base_idx = s_base_idx + slot * n_seq * (nb03 / 4u);
[[unroll]] for (uint j = 0; j < c_factor; j++) {
d[snapshot_base_idx + SUBGROUP_SIZE * j + lane] = state[j];
if (D_STATE % SUBGROUP_SIZE == 0 || SUBGROUP_SIZE * j + lane < D_STATE) {
d[snapshot_base_idx + SUBGROUP_SIZE * j + lane] = state[j];
}
}
}
}
@@ -129,6 +134,8 @@ void main() {
// write back the state
[[unroll]]
for (int j = 0; j < c_factor; j++) {
d[s_base_idx + SUBGROUP_SIZE * j + lane] = state[j];
if (D_STATE % SUBGROUP_SIZE == 0 || SUBGROUP_SIZE * j + lane < D_STATE) {
d[s_base_idx + SUBGROUP_SIZE * j + lane] = state[j];
}
}
}
@@ -1,6 +1,7 @@
#version 450
#include "types.glsl"
#include "utils.glsl"
#if defined(UNARY_MUL_FUSION)
#include "generic_binary_head.glsl"
#else
@@ -116,7 +117,7 @@ float op_expm1(float x) {
}
float op_softplus(float x) {
return (x > 20.0f) ? x : log(1.0f + exp(x));
return (x > 20.0f) ? x : log1p(exp(x));
}
float op_gelu_erf(float a) {
@@ -25,6 +25,26 @@ uint fastdiv_small(uint a, uint b) {
return (a < b) ? 0 : (a / b);
}
// log(1 + x); the atanh series 2*y*(1 + y^2/3 + ... + y^12/13), y = x/(2+x),
// keeps the precision that log(1 + x) loses for x in [-0.5, 1].
float log1p(float x) {
float y = x / (2.0f + x);
float y2 = y * y;
float p = 1.0f / 13.0f;
p = p * y2 + 1.0f / 11.0f;
p = p * y2 + 1.0f / 9.0f;
p = p * y2 + 1.0f / 7.0f;
p = p * y2 + 1.0f / 5.0f;
p = p * y2 + 1.0f / 3.0f;
p = p * y2 + 1.0f;
float s = 2.0f * y * p;
float l = log(1.0f + x);
bool in_series = (x >= -0.5f) && (x <= 1.0f);
bool tiny = abs(x) < 2.5e-38f;
float r = in_series ? s : l;
return tiny ? x : r;
}
void get_indices(uint idx, out uint i00, out uint i01, out uint i02, out uint i03, uint ne00, uint ne01, uint ne02, uint ne03) {
i03 = fastdiv_small(idx, (ne02*ne01*ne00));
const uint i03_offset = i03 * ne02*ne01*ne00;
+17 -1
View File
@@ -2258,6 +2258,20 @@ struct test_unary : public test_case {
};
struct test_unary_softplus_neg : public test_unary {
test_unary_softplus_neg() : test_unary(GGML_UNARY_OP_SOFTPLUS) {}
std::string vars() override {
return test_unary::vars() + ",neg";
}
void initialize_tensors(ggml_context * ctx) override {
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
init_tensor_uniform(t, -16.6f, -16.0f);
}
}
};
// GGML_OP_GLU
struct test_glu : public test_case {
const ggml_glu_op op;
@@ -11130,7 +11144,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
std::default_random_engine rng(0);
// unary ops
// TODO: add GGML_UNARY_OP_SOFTPLUS test that is sensitive to using `log1p(exp(x))` vs `log(exp(x) + 1)`
test_cases.emplace_back(new test_unary_softplus_neg());
for (ggml_type type : {GGML_TYPE_F16, GGML_TYPE_F32, GGML_TYPE_BF16}) {
for (int v : {0, 1}) {
for (int op = 0; op < GGML_UNARY_OP_COUNT; op++) {
@@ -14854,6 +14868,8 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {4, 3328, 1, 1}, {4, 3328, 1, 1})); // generate
test_cases.emplace_back(new test_ssm_conv_bias_silu(GGML_TYPE_F32, {515, 3328, 1, 1}, {4, 3328, 1, 1}, true)); // prefill
test_cases.emplace_back(new test_ssm_conv_bias_silu(GGML_TYPE_F32, {4, 3328, 1, 1}, {4, 3328, 1, 1}, true)); // generate
test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 96, 64, 128, 8, 512, 1)); // Nemotron-3-Puzzle prefill
test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 96, 64, 128, 8, 1, 1)); // Nemotron-3-Puzzle generate
test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 48, 1, 512, 1)); // prefill
test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 64, 48, 1, 1, 1)); // generate
test_cases.emplace_back(new test_ssm_scan(GGML_TYPE_F32, 128, 80, 128, 1, 512, 1)); // Nemotron-9B prefill
@@ -29,7 +29,7 @@
</script>
<span
class="text-caption font-normal text-muted-foreground tabular-nums"
class="text-body-sm font-normal text-muted-foreground tabular-nums"
title="Time since the task started"
>
{label}
@@ -11,6 +11,7 @@
import { conversationsStore, modelsStore, serverStore } from '#lib/stores/index.ts';
import type { DatabaseMessage } from '#lib/types/database.js';
import { getConversationModel } from '#lib/utils/index.ts';
import { getChatFormModelState } from '#lib/utils/model-selection.ts';
export function useChatFormModel() {
const isRouter = $derived(serverStore.isRouterMode);
@@ -82,22 +83,17 @@ export function useChatFormModel() {
return activeModelId ? modelsStore.props.modelSupportsVision(activeModelId) : false;
});
const hasModelSelected = $derived(
!isRouter || !!conversationModel || !!modelsStore.selectedModelId
// the form's selection decides the state; the conversation's model only counts when nothing is selected
const modelState = $derived(
getChatFormModelState({
conversationModel,
isRouter,
models: modelsStore.models,
selectedModelId: modelsStore.selectedModelId
})
);
const isSelectedModelInCache = $derived.by(() => {
if (!isRouter) return true;
if (conversationModel) {
return modelsStore.models.some((option) => option.model === conversationModel);
}
const currentModelId = modelsStore.selectedModelId;
if (!currentModelId) return false;
return modelsStore.models.some((option) => option.id === currentModelId);
});
const hasModelSelected = $derived(modelState.hasModelSelected);
const isSelectedModelInCache = $derived(modelState.isSelectedModelAvailable);
return {
get conversationModel() {
@@ -0,0 +1,43 @@
import type { ModelOption } from '#lib/types/models.js';
export interface ChatFormModelStateInput {
conversationModel: string | null;
isRouter: boolean;
models: readonly Pick<ModelOption, 'id' | 'model'>[];
selectedModelId: string | null;
}
export interface ChatFormModelState {
hasModelSelected: boolean;
isSelectedModelAvailable: boolean;
}
/**
* Whether the chat form has a model to send with and whether that model is available.
* The form's own selection always wins; the conversation's last model only counts
* when nothing is selected in the form.
*/
export function getChatFormModelState({
conversationModel,
isRouter,
models,
selectedModelId
}: ChatFormModelStateInput): ChatFormModelState {
if (!isRouter) return { hasModelSelected: true, isSelectedModelAvailable: true };
if (selectedModelId) {
return {
hasModelSelected: true,
isSelectedModelAvailable: models.some((option) => option.id === selectedModelId)
};
}
if (conversationModel) {
return {
hasModelSelected: true,
isSelectedModelAvailable: models.some((option) => option.model === conversationModel)
};
}
return { hasModelSelected: false, isSelectedModelAvailable: false };
}
@@ -0,0 +1,84 @@
// Regression: when the conversation's last assistant message names a model that is no
// longer available, picking an available model in the chat form must clear the submit
// button's error state. The form's selection always decides it.
import ChatFormTestWrapper from './components/ChatFormTestWrapper.svelte';
import { MessageRole, ServerRole } from '#lib/enums/index.ts';
import { conversationsStore } from '#lib/stores/conversations/index.svelte.ts';
import { serverStore } from '#lib/stores/llama-server.svelte.ts';
import { modelsStore } from '#lib/stores/models/index.svelte.ts';
import type { DatabaseMessage, ModelOption } from '#lib/types/index.ts';
import { tick } from 'svelte';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { render } from 'vitest-browser-svelte';
const AVAILABLE: ModelOption = {
capabilities: [],
id: 'org/available:Q4_K_M',
model: 'org/available:Q4_K_M',
name: 'org/available:Q4_K_M'
};
const GONE = 'org/gone:Q4_K_M';
function assistantMessage(model: string): DatabaseMessage {
return {
children: [],
content: 'Hello',
convId: 'c1',
id: 'm1',
model,
parent: null,
role: MessageRole.ASSISTANT,
timestamp: 0,
type: 'text'
} as DatabaseMessage;
}
function sendButton(container: HTMLElement) {
const button = container.querySelector<HTMLButtonElement>('button[type="submit"]');
if (!button) throw new Error('send button not rendered');
return button;
}
const isErrorState = (button: HTMLButtonElement) => button.classList.contains('bg-surface-danger');
beforeEach(() => {
serverStore.role = ServerRole.ROUTER;
modelsStore.models = [AVAILABLE];
modelsStore.clearSelection();
conversationsStore.activeMessages = [assistantMessage(GONE)];
});
afterEach(() => {
conversationsStore.activeMessages = [];
modelsStore.clearSelection();
modelsStore.models = [];
});
describe('ChatForm model availability', () => {
it('shows the error state while the only model is the conversation one that is gone', async () => {
const { container } = render(ChatFormTestWrapper, { initialValue: 'Hi' });
await tick();
const button = sendButton(container);
expect(isErrorState(button)).toBe(true);
expect(button.disabled).toBe(true);
});
it('clears the error state once an available model is selected in the form', async () => {
const { container } = render(ChatFormTestWrapper, { initialValue: 'Hi' });
await tick();
await modelsStore.selectModelById(AVAILABLE.id);
await tick();
const button = sendButton(container);
expect(isErrorState(button)).toBe(false);
expect(button.disabled).toBe(false);
});
});
@@ -0,0 +1,70 @@
// Guards which model decides the chat form's submit state: the form's own selection
// always wins over the model of the conversation's last assistant message.
import { getChatFormModelState } from '#lib/utils/model-selection.ts';
import { describe, expect, it } from 'vitest';
const AVAILABLE = { id: 'org/available:Q4_K_M', model: 'org/available:Q4_K_M' };
const GONE = 'org/gone:Q4_K_M';
describe('getChatFormModelState', () => {
it('reports an available model when the form selects one, even if the conversation used a gone model', () => {
expect(
getChatFormModelState({
conversationModel: GONE,
isRouter: true,
models: [AVAILABLE],
selectedModelId: AVAILABLE.id
})
).toEqual({ hasModelSelected: true, isSelectedModelAvailable: true });
});
it('reports an unavailable model when the form selects one that left the list', () => {
expect(
getChatFormModelState({
conversationModel: AVAILABLE.model,
isRouter: true,
models: [AVAILABLE],
selectedModelId: GONE
})
).toEqual({ hasModelSelected: true, isSelectedModelAvailable: false });
});
it('falls back to the conversation model when nothing is selected in the form', () => {
const state = (conversationModel: string) =>
getChatFormModelState({
conversationModel,
isRouter: true,
models: [AVAILABLE],
selectedModelId: null
});
expect(state(GONE)).toEqual({ hasModelSelected: true, isSelectedModelAvailable: false });
expect(state(AVAILABLE.model)).toEqual({
hasModelSelected: true,
isSelectedModelAvailable: true
});
});
it('has no model selected on a router with neither a selection nor a conversation model', () => {
expect(
getChatFormModelState({
conversationModel: null,
isRouter: true,
models: [AVAILABLE],
selectedModelId: null
})
).toEqual({ hasModelSelected: false, isSelectedModelAvailable: false });
});
it('always has an available model outside router mode', () => {
expect(
getChatFormModelState({
conversationModel: GONE,
isRouter: false,
models: [],
selectedModelId: null
})
).toEqual({ hasModelSelected: true, isSelectedModelAvailable: true });
});
});