ds4-server: optional --imatrix-out to collect the imatrix from live traffic?
Before opening a PR I'd rather ask, since it touches the serving path.
Proposal: ds4-server --imatrix-out FILE reuses the existing ds4_imatrix_collector and the imatrix_collect_layer_batch hook already in metal_graph_prefill_layer_major, accumulating the routed-MoE imatrix from live prefills and writing it on shutdown. No new backend code — a ds4_session field, a couple of public wrappers, and the prefill call-sites passing the collector. Default off is byte-identical (the field stays NULL unless --imatrix-out is given). The file holds only per-expert second moments + counts — no prompt text, ids, or positions.
Why on the server and not just ds4 --imatrix-dataset: the offline path needs you to materialize a prompt dataset on disk first. When prompts can't be written to disk or leave the machine for compliance reasons — e.g. patient data under GDPR — that path isn't usable, but collecting aggregate stats in-process is, since no prompt content ever reaches disk. That's the case where the server entry point isn't redundant with the CLI.
Cost: while enabled it forces the layer-major readback (suppresses the streaming-decode fast path), so prefill is slower; decode unaffected; opt-in.
Does this fit ds4, or would you rather keep imatrix collection CLI-only?
A field note after running this collector for a few days of benchmark serving (on my fork's implementation, but the property comes from the hook design itself so it applies to any version of this proposal).
The collector is not output neutral. With --imatrix-out enabled the generated tokens can differ from collector-off serving, same prompt, same flags, greedy at temperature 0.
The reason is that the streaming prefill fast path never materializes the per-layer activations the collector needs to read, so enabling it routes prefill through the layer-major readback path (the same reason for the ~+31% prefill cost). Different kernels and accumulation order means the logits come out different at float rounding scale. Usually irrelevant, but when the top two logits are nearly tied the argmax flips, and from that token on the whole greedy generation diverges.
I hit this in practice and confirmed it with a single variable A/B: I had a generation produced with the collector on, tried to reproduce it with the collector off and it failed a token-exact prefix check right at a near tie. Re-running with the collector on reproduced the original token by token. So determinism holds perfectly per configuration, the collector is simply part of the configuration.
Not a correctness bug, both outputs are legitimate greedy results under slightly different arithmetic. But it deserves a line in the docs, something like: "with the collector enabled, generated tokens may occasionally differ from collector-off serving; benchmark with the same collector state you serve with". Without it anyone comparing capture-on vs capture-off runs token by token will chase phantom non-determinism. I did, for a whole evening :)