Repository navigation
Description
Summary
mlxcel-server panics and aborts (Abort trap: 6) when a request to /v1/completions or /v1/chat/completions asks for top-k logprobs (for example "logprobs": 5) against a model whose logits / log-probs are f16 or bf16. After the #289 change ("keep quantized models fully bf16"), that is most quantized checkpoints, so this hits the common case.
Observed panic:
range end index 12 out of range for slice of length 10
src/lib/mlxcel-core/src/sampling.rs:722
This is pre-existing and unrelated to PR #339 (#325); see Provenance below.
Reproduce
./target/release/mlxcel-server -m models/qwen3-0.6b-4bit --parallel 8 --port 8131- POST
/v1/completionswith{"prompt":"...","max_tokens":32,"temperature":0,"logprobs":5}
The worker thread panics and the process aborts. qwen3-0.6b-4bit carries bf16 logits under the current #289 policy, so any top_k >= 1 triggers it.
Root cause
compute_logprobs() in src/lib/mlxcel-core/src/sampling.rs (top-k branch, lines 714-725) extracts the top-k log-probabilities with array_to_raw_bytes plus a hardcoded 4-byte stride:
let idx_bytes = ffi::array_to_raw_bytes(&top_idx); // int32 indices: 4 bytes/elem
let lp_bytes = ffi::array_to_raw_bytes(&top_lp); // f16/bf16 logprobs: 2 bytes/elem
let mut pairs: Vec<(i32, f32)> = (0..k_usize.min(idx_bytes.len() / 4))
.filter_map(|i| {
let tok_bytes: [u8; 4] = idx_bytes[i * 4..(i + 1) * 4].try_into().ok()?;
let lp_bytes4: [u8; 4] = lp_bytes[i * 4..(i + 1) * 4].try_into().ok()?; // line 722: panics
Some((i32::from_ne_bytes(tok_bytes), f32::from_ne_bytes(lp_bytes4)))
})
.collect();array_to_raw_bytes (cpp/mlx_cxx_bridge.cpp:301) dumps the array verbatim, a.nbytes() bytes, with no dtype conversion. top_lp = take_along_axis(log_probs, top_idx) inherits the log-softmax dtype, which inherits the model logit dtype. For f16/bf16 that is 2 bytes per element, so lp_bytes.len() == 2 * k. But:
- the loop bound is
idx_bytes.len() / 4, driven by the int32 index array (genuinely 4 bytes per element), so the bound equalsk; - the
lp_bytesread uses the same 4-byte stride against a 2-byte-per-element buffer.
For logprobs: 5 and f16/bf16 logits: idx_bytes is 20 bytes (bound 5), lp_bytes is 10 bytes. At i = 2 the slice lp_bytes[8..12] runs past length 10, which is exactly the reported panic. The no-overrun condition 4*(k-1) + 4 <= 2*k reduces to k <= 0, so the slice overruns for any top_k >= 1: on a bf16 model, top-k logprobs always crashes. Even if the read did not overrun, reinterpreting two f16 elements as one f32 would yield garbage logprob values.
Note: the tok_bytes read is correct (indices really are 4 bytes). Only the logprob read is wrong.
Selected-token logprob path: verified correct (does NOT share the bug)
Checked end-to-end per the request. The selected-token logprob (lines 680-684) does NOT use raw bytes:
let selected_lp_arr = ffi::take_along_axis(&log_probs, &idx, -1);
ffi::eval(&selected_lp_arr);
let selected_logprob = ffi::item_f32(&selected_lp_arr);item_f32 (cpp/mlx_cxx_bridge.cpp:285) calls MLX array::item<float>(), which is dtype-aware and converts the stored value to f32 regardless of f16/bf16/f32 storage. So the always-on selected-token logprob is correct on f16/bf16 models, and logprobs requests without top-k (top_k == 0) neither crash nor corrupt. The crash and the corruption are confined to the top-k alternatives path.
Why existing tests miss it
The unit tests for this function (compute_logprobs_returns_top_k_alternatives_sorted_descending at sampling.rs:1100, compute_logprobs_top_k_capped_at_vocab_size at sampling.rs:1116) build logits with ffi::from_slice_f32, i.e. f32 (4 bytes), where the 4-byte stride is correct by accident. No test exercises an f16/bf16 logit array, so the dtype mismatch is invisible to CI.
Provenance
git blame -L 718,726 src/lib/mlxcel-core/src/sampling.rs attributes the block to commit 892b24288a (2026-04-02, "feat: implement logprobs support for chat completions and completions endpoints"). It predates and is unrelated to PR #339 (#325); it surfaced only during real-model validation of that PR.
Suggested fix direction (not to implement in this issue)
At the logprobs boundary, either:
- cast
top_lpto f32 with an explicitastypebeforearray_to_raw_bytes, so the 4-byte stride is valid; or - read the element size from the array dtype instead of hardcoding 4, and decode f16/bf16 accordingly.
Per project policy, quantized models keep bf16 scales/biases and (post-#289) bf16 logits, so the fix must handle bf16 logits at the logprobs boundary rather than promoting model weights. The selected-token path already does the right thing via item<float>(); the top-k path should reach the same f32 values.
Acceptance Criteria
- Top-k logprobs return correct f32 values for f16, bf16, and f32 logit models.
- Selected-token logprob remains correct across f16/bf16/f32 (guard the current behavior so a refactor cannot regress it).
-
/v1/completionsand/v1/chat/completionswithlogprobs: Nno longer crash on a bf16 model (for exampleqwen3-0.6b-4bit). - A regression test covers the f16/bf16 logprobs path (both top-k and selected token), not only f32.
Activity
fix(sampling): cast selected-token logprob to f32 before item_f32 (#340)
style(sampling): rustfmt the new f16/bf16 logprobs tests
fix(sampling): correct f16/bf16 logprobs crash and corruption (#340)
release: v0.3.2
fix(sampling): correct f16/bf16 logprobs crash and corruption (lablup…
release: v0.3.2