From 56c93ab89ea10c63abba9f9e5154050b742fc5da Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 5 May 2026 15:57:17 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[performance=20improvement]?= =?UTF-8?q?=20Replace=20O(N^2)=20list=20pop(0)=20in=20entropy=20calculatio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .jules/bolt.md | 3 +++ fastdeploy/model_executor/entropy_utils.py | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000000..e85ac3ab0d3 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2025-05-05 - Replacing O(N^2) list pop(0) in fastdeploy/model_executor/entropy_utils.py +**Learning:** Found O(N^2) list operations where `entropy.pop(0)` was called inside nested loops over batch size and sequence lengths. +**Action:** Replace `pop(0)` within a loop with an index tracker and bulk addition via `.extend(list[start:end])`. diff --git a/fastdeploy/model_executor/entropy_utils.py b/fastdeploy/model_executor/entropy_utils.py index 21d1b3421e9..9fd2605775f 100644 --- a/fastdeploy/model_executor/entropy_utils.py +++ b/fastdeploy/model_executor/entropy_utils.py @@ -49,9 +49,13 @@ def calculate_logits_entropy(logits, share_inputs, temperature): entropy_tensor = get_entropy(logits) entropy = entropy_tensor.tolist() + entropy_idx = 0 for i in range(real_bsz): - for _ in range(real_seq_lens[i]): - share_inputs["entropy_list"][i].append(entropy.pop(0)) + seq_len = int(real_seq_lens[i]) + if seq_len > 0: + share_inputs["entropy_list"][i].extend(entropy[entropy_idx : entropy_idx + seq_len]) + entropy_idx += seq_len + if ( share_inputs["stop_flags"][i] and share_inputs["seq_lens_decoder"][i] != 0 @@ -92,9 +96,13 @@ def speculate_calculate_logits_entropy(logits, share_inputs, temperature): entropy_tensor = get_entropy(accepted_logits) entropy = entropy_tensor.tolist() + entropy_idx = 0 for i in range(real_bsz): - for _ in range(share_inputs["accept_num"][i]): - share_inputs["entropy_list"][i].append(entropy.pop(0)) + accept_num = int(share_inputs["accept_num"][i]) + if accept_num > 0: + share_inputs["entropy_list"][i].extend(entropy[entropy_idx : entropy_idx + accept_num]) + entropy_idx += accept_num + if ( share_inputs["stop_flags"][i] and share_inputs["seq_lens_decoder"][i] != 0