Skip to content

ggml-openvino : FILL for in-place clear of recurrent cache, sticky NaN fix - #31977

Closed
sanmai wants to merge 8 commits into
ggml-org:masterfrom
sanmai:openvino-rs-reset-fill
Closed

sanmai wants to merge 8 commits into
ggml-org:masterfrom
sanmai:openvino-rs-reset-fill

Conversation

@sanmai

@sanmai sanmai commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Overview

This PR adds support for FILL as the in-place clear and also resolves a sticky NaN situation.

Additional information

PR #31913 replaces ggml_scale with ggml_fill in a pair of places making FILL a necessity.

The reproducer for the sicky NaN: https://gist.github.com/sanmai/d37a6deed093dc896a116a4645e67c9e

Without the fix it breaks down as:

== healthy
The capital of France is ->
"content":" Paris.\nThe capital of"
"logprob":-2.33124041557312
"n_restored":10
== the restored prompt, extended: the slot keeps the restored state
The capital of France is Paris ->
"content":"??????"
"logprob":null

And then corrupts all future responses.

With the fix:

== healthy
The capital of France is ->
"content":" Paris.\nThe capital of"
"logprob":-2.33124041557312
"n_restored":10
== the restored prompt, extended: the slot keeps the restored state
The capital of France is Paris ->
"content":".\nThe capital of France"
"logprob":-0.6966474652290344

I tested it both with GGML_OPENVINO_DEVICE=CPU and GGML_OPENVINO_DEVICE=GPU.0 (iGPU).

Prior work:

Related PRs and issues:

Requirements

@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning OpenVINO labels Nov 9, 2026

// Select, not Multiply by the mask: 0 * NaN is NaN, so a slot with NaN would stay NaN
auto zero = ov::op::v0::Constant::create(cache_rs.get_element_type(), ov::Shape{}, {0});
auto cleared_cache_rs = std::make_shared<ov::op::v1::Select>(keep_mask_reshape, cache_rs, zero);

@sanmai sanmai Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the remainder of this function is unchanged compared to the original implementation


if (is_recurrent_cache_reset_case(context)) {
return translate_recurrent_cache_reset(context);
}

@sanmai sanmai Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache_rs_reset_len assumes that c is 0.0 so ignoring its value here is safe

bool is_inplace_op(const ggml_tensor * node) {
return node->op == GGML_OP_SET_ROWS || node->op == GGML_OP_CPY || (node->op == GGML_OP_SCALE && node->view_src);
const bool is_scale_or_fill = node->op == GGML_OP_SCALE || node->op == GGML_OP_FILL;
return node->op == GGML_OP_SET_ROWS || node->op == GGML_OP_CPY || (is_scale_or_fill && node->view_src);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GGML_OP_FILL here to support ggml_fill_inplace in addition to ggml_scale_inplace


inline static bool is_recurrent_cache_reset(const ggml_tensor * node) {
const bool is_scale_or_fill = node->op == GGML_OP_SCALE || node->op == GGML_OP_FILL;
return is_scale_or_fill && node->view_src != nullptr && is_recurrent_cache(node->view_src);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node->view_src != nullptr is not required here as is_recurrent_cache checks for that too, but I kept it here to align with the remainder of the file (probably worth a cleanup still)

@sanmai sanmai changed the title ggml-openvino : support FILL as the in-place clear of the recurrent cache ggml-openvino : FILL for in-place clear of recurrent cache, sticky NaN fix Nov 9, 2026
@sanmai

sanmai commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

/bot review

@ggml-gh-bot

ggml-gh-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Automated code review

Review of PR #31977 — ggml-openvino: FILL as in-place clear of the recurrent cache, sticky NaN fix.

Summary: The change routes GGML_OP_FILL through the same recurrent-cache-reset machinery as GGML_OP_SCALE (op_case, param extraction, single-slot buffer clear), factors the reset translation out of scale.cpp into a shared translate_recurrent_cache_reset, and replaces the keep-mask Multiply with a Select so a NaN in a cleared slot becomes a true zero instead of 0 * NaN = NaN. The refactor is behavior-preserving for SCALE (verified: op_case 2 returns the input unrenamed exactly as before; op_case 1 previously required has_input("cache_rs_reset_len"), same as the new helper), and FILL's graph shape (ggml_view_tensor result, view_src chained to the cache, src[0] holding the offset view) is identical to SCALE's, so the existing detection paths carry over unchanged. The NaN fix is correct and well-placed. No blocking findings.

Will slow the review / hardening suggestions

(point 1) ggml-decoder.cpp:637-641 — the op_case assignment for SCALE/FILL keys only on view_src shape and buffer usage, not on the cache identity. With FILL added, any future ggml_fill_inplace on a 1-row view of a compute-buffer tensor would be translated as an identity (the fill silently dropped), and any multi-row in-place fill in a graph that also has a recurrent reset would be translated as a slot clear. Not reachable today (ggml_fill_inplace has no callers until the llama.cpp side of #31913 lands), but since this PR introduces is_recurrent_cache_reset, consider gating op_case 1/2 on is_recurrent_cache(node->view_src) so only genuine cache resets take the reset path.

(point 2) The reset translation ignores the fill/scale constant — is_recurrent_cache_reset (ggml-decoder.h:411) could also require the constant to be zero (both ops store the value in op_params[0]), so a nonzero ggml_scale/ggml_fill on a cache view is not silently converted into a zero-clear.

(point 3) Pre-existing, adjacent, worth verifying: ggml-decoder.cpp:1001 computes cache_rs_reset_idx = node->src[0]->view_offs / node->view_src->ne[0] — that divides a byte offset by an element count. The neighboring writeback code divides correctly by row bytes (row_bytes = cache->ne[0] * ggml_type_size(cache->type)), so for f32/f16 caches the reset index looks 2-4x the actual slot index. Untouched by this diff, but FILL support now routes through the same line; if the reproducer's multi-slot rollback run never hits rs_z != 0, this could be latent. Suggested form: view_offs / node->view_src->nb[1].

(point 4) The PR template's disclosure section says "YES" but does not describe how AI was used — please complete that per the template.

Nits

(point 5) openvino/op/scale.cpp — after moving the reset code out, equal.hpp, if.hpp, concat.hpp, reshape.hpp, slice.hpp (and core/except.hpp) are no longer used by this file and could go too.

(point 6) ggml-decoder.h:410 — inline static is redundant inside a class; the neighboring helpers use plain static. Purely cosmetic.

Context notes: the FILL paths are unexercised until #31913 switches src/llama-graph.cpp:4028 to a fill — both PRs reference each other, which is good for reviewers. The SCALE-side Multiply -> Select change is exercisable today and is what the reproducer validates. Comments updated where they referenced the moved code (add_extra_inputs, get_ov_input_tensor_static_prefill) are accurate, and the new comments follow the concise why-only style with ASCII only.

This review was generated automatically by pi coding agent using zai-org/GLM-5.3. It may contain mistakes. Maintainers make the final call.

}
compute_params.cache_rs_reset_len = ggml_nelements(node) / node->view_src->ne[0];
compute_params.cache_rs_reset_idx = node->src[0]->view_offs / node->view_src->ne[0];
compute_params.cache_rs_reset_idx = node->src[0]->view_offs / node->view_src->nb[1];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bot found this so I fixed it while we are at it (it divided by the number of elements, where it should have divided by the bytes per row)

@sanmai
sanmai marked this pull request as ready for review November 10, 2026 01:24
@sanmai
sanmai requested a review from wine99 as a code owner November 10, 2026 01:24
@sanmai
sanmai requested a review from a team November 10, 2026 06:51
@sanmai sanmai self-assigned this Nov 11, 2026
@sanmai

sanmai commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

@ravi9 What will it take to review this PR? Thanks!

@ravi9

ravi9 commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Hi @sanmai, thank you for this PR. We just opened PR #32172 .
We review it once it merges, or if you can rebase and raise a PR into https://github.com/ravi9/llama.cpp/tree/pr-2026-11-12 , we can merge it via #32172

@sanmai

sanmai commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

@ravi9 Raised ravi9#425, thanks!

@sanmai
sanmai deleted the openvino-rs-reset-fill branch November 16, 2026 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning OpenVINO

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants