From 1aa75ce0eeef2e71c0dd47faafe0f558ddc9d1a7 Mon Sep 17 00:00:00 2001 From: Eylon Krause Date: Fri, 3 Jul 2026 23:55:58 +0300 Subject: [PATCH] prepacked_cache: fix byte-accounting for evicted float matrices AllocateBuffers only allocates and counts a `sums` buffer for non-floating-point matrices; float matrices never allocate one, so a float insertion only adds DataBytes to buffers_bytes_. EjectOne, however, unconditionally subtracted DataBytes + SumsBytes. A float matrix's sums_type still reports a nonzero element size, so SumsBytes is nonzero and each float eviction over-subtracted it, letting buffers_bytes_ drift below true usage (possibly negative) so the cache silently overshoots max_buffers_bytes_. Subtract SumsBytes only for non-floating-point matrices, mirroring AllocateBuffers. --- ruy/prepacked_cache.cc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ruy/prepacked_cache.cc b/ruy/prepacked_cache.cc index 5080ca9722..393fe48a45 100644 --- a/ruy/prepacked_cache.cc +++ b/ruy/prepacked_cache.cc @@ -121,7 +121,16 @@ void PrepackedCache::EjectOne() { } } const PEMat& packed_matrix = oldest->second.packed_matrix; - buffers_bytes_ -= DataBytes(packed_matrix) + SumsBytes(packed_matrix); + // Mirror the accounting done in AllocateBuffers: floating-point matrices do + // not allocate a `sums` buffer, so only their `data` bytes were ever added + // to buffers_bytes_. Unconditionally subtracting SumsBytes here would + // over-subtract for float matrices (whose sums_type still has nonzero size), + // letting buffers_bytes_ drift below the true usage and the cache overshoot + // its cap. + buffers_bytes_ -= DataBytes(packed_matrix); + if (!packed_matrix.sums_type.is_floating_point) { + buffers_bytes_ -= SumsBytes(packed_matrix); + } FreeBuffers(packed_matrix); cache_.erase(oldest); }